Skip to content

Commit 5800bf3

Browse files
chore(repo): test migration to vitest. phase 4 (#1978)
* chore(repo): test migration to vitest. phase 4 * fix: address vitest phase 4 review follow-ups * test: fix vitest phase-4 lint and tslib snapshot --------- Co-authored-by: CharlieHelps <charlie@charlielabs.ai>
1 parent 1cfa530 commit 5800bf3

43 files changed

Lines changed: 2507 additions & 2400 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/commonjs/package.json

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
"ci:coverage": "nyc pnpm test && nyc report --reporter=text-lcov > coverage.lcov",
2929
"ci:lint": "pnpm build && pnpm lint",
3030
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
31-
"ci:test": "pnpm test -- --verbose && pnpm test:ts",
31+
"ci:test": "pnpm test -- --reporter=verbose && pnpm test:ts",
3232
"prebuild": "del-cli dist",
3333
"prepare": "if [ ! -d 'dist' ]; then pnpm build; fi",
3434
"prepublishOnly": "pnpm build",
3535
"prerelease": "pnpm build",
3636
"pretest": "pnpm build",
3737
"release": "pnpm --workspace-root package:release $(pwd)",
38-
"test": "ava",
38+
"test": "vitest --config ../../.config/vitest.config.mts run",
3939
"test:ts": "tsc types/index.d.ts test/types.ts --noEmit"
4040
},
4141
"files": [
@@ -81,14 +81,5 @@
8181
"source-map-support": "^0.5.21",
8282
"typescript": "^4.8.3"
8383
},
84-
"types": "./types/index.d.ts",
85-
"ava": {
86-
"workerThreads": false,
87-
"files": [
88-
"!**/fixtures/**",
89-
"!**/helpers/**",
90-
"!**/recipes/**",
91-
"!**/types.ts"
92-
]
93-
}
84+
"types": "./types/index.d.ts"
9485
}

packages/commonjs/test/form.js

Lines changed: 78 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@ const fs = require('fs');
44
const path = require('path');
55

66
const acorn = require('acorn');
7-
const test = require('ava');
87

98
const { commonjs } = require('./helpers/util.js');
109

1110
process.chdir(__dirname);
12-
1311
const transformContext = {
1412
error: (base, props) => {
1513
let error = base;
1614
if (!(base instanceof Error)) error = Object.assign(new Error(base.message), base);
1715
if (props) Object.assign(error, props);
1816
throw error;
1917
},
20-
load: ({ id }) => Promise.resolve({ id, meta: {} }),
18+
load: ({ id }) =>
19+
Promise.resolve({
20+
id,
21+
meta: {}
22+
}),
2123
parse: (input, options) =>
2224
acorn.parse(input, {
2325
ecmaVersion: 9,
@@ -30,78 +32,81 @@ const transformContext = {
3032
})
3133
};
3234

33-
// Do not run on Windows as we have full path names in the output
34-
if (path.sep === '/') {
35-
fs.readdirSync('./fixtures/form').forEach((dir) => {
36-
let config;
37-
38-
try {
39-
config = require(`./fixtures/form/${dir}/_config.js`);
40-
} catch (err) {
41-
config = {};
42-
}
43-
44-
const inputEntries = [];
35+
const runFormTest = test.skipIf(path.sep !== '/');
4536

46-
if (typeof config.multi === 'object') {
47-
for (const [key, entry] of Object.entries(config.multi)) {
48-
inputEntries.push([key, `fixtures/form/${dir}/${entry}`]);
49-
}
50-
} else {
51-
inputEntries.push(['output', `fixtures/form/${dir}/input.js`]);
37+
fs.readdirSync('./fixtures/form').forEach((dir) => {
38+
let config;
39+
try {
40+
config = require(`./fixtures/form/${dir}/_config.js`);
41+
} catch (err) {
42+
config = {};
43+
}
44+
const inputEntries = [];
45+
if (typeof config.multi === 'object') {
46+
for (const [key, entry] of Object.entries(config.multi)) {
47+
inputEntries.push([key, `fixtures/form/${dir}/${entry}`]);
5248
}
53-
54-
(config.solo ? test.only : test)(dir, (t) =>
55-
Promise.all(
56-
inputEntries.map(async ([outputName, id]) => {
57-
const { buildStart, transform } = commonjs(config.options);
58-
buildStart.call({ meta: { rollupVersion: '99.0.0' } }, { plugins: [] });
59-
transformContext.getModuleInfo = (moduleId) => {
60-
return {
61-
isEntry: config.entry && moduleId === id,
62-
importers:
63-
config.importers && config.importers[outputName]
64-
? config.importers[outputName].map((x) => `fixtures/form/${dir}/${x}`)
65-
: [],
66-
meta: {}
67-
};
68-
};
69-
const input = fs.readFileSync(id, 'utf-8');
70-
71-
let outputFile = `fixtures/form/${dir}/${outputName}`;
72-
if (fs.existsSync(`${outputFile}.${process.platform}.js`)) {
73-
outputFile += `.${process.platform}.js`;
74-
} else {
75-
outputFile += '.js';
76-
}
77-
78-
const expected = fs.readFileSync(outputFile, 'utf-8').trim();
79-
// eslint-disable-next-line no-await-in-loop
80-
const transformed = await transform.call(transformContext, input, id);
81-
let actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_');
82-
const cwd = process.cwd();
83-
while (actual.indexOf(cwd) >= 0) {
84-
actual = actual.replace(process.cwd(), 'CWD');
49+
} else {
50+
inputEntries.push(['output', `fixtures/form/${dir}/input.js`]);
51+
}
52+
(config.solo ? runFormTest.only : runFormTest)(dir, () =>
53+
Promise.all(
54+
inputEntries.map(async ([outputName, id]) => {
55+
const { buildStart, transform } = commonjs(config.options);
56+
buildStart.call(
57+
{
58+
meta: {
59+
rollupVersion: '99.0.0'
60+
}
61+
},
62+
{
63+
plugins: []
8564
}
65+
);
66+
transformContext.getModuleInfo = (moduleId) => {
67+
return {
68+
isEntry: config.entry && moduleId === id,
69+
importers:
70+
config.importers && config.importers[outputName]
71+
? config.importers[outputName].map((x) => `fixtures/form/${dir}/${x}`)
72+
: [],
73+
meta: {}
74+
};
75+
};
76+
const input = fs.readFileSync(id, 'utf-8');
77+
let outputFile = `fixtures/form/${dir}/${outputName}`;
78+
if (fs.existsSync(`${outputFile}.${process.platform}.js`)) {
79+
outputFile += `.${process.platform}.js`;
80+
} else {
81+
outputFile += '.js';
82+
}
83+
const expected = fs.readFileSync(outputFile, 'utf-8').trim();
84+
// eslint-disable-next-line no-await-in-loop
85+
const transformed = await transform.call(transformContext, input, id);
86+
let actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_');
87+
const cwd = process.cwd();
88+
while (actual.indexOf(cwd) >= 0) {
89+
actual = actual.replace(process.cwd(), 'CWD');
90+
}
8691

87-
// uncomment to update snapshots
88-
// fs.writeFileSync(outputFile, `${actual}\n`);
92+
// uncomment to update snapshots
93+
// fs.writeFileSync(outputFile, `${actual}\n`);
8994

90-
// trim whitespace from line endings,
91-
// this will benefit issues like `form/try-catch-remove` where whitespace is left in the line,
92-
// and testing on windows (\r\n)
93-
t.is(
94-
actual
95-
.split('\n')
96-
.map((x) => x.trimEnd())
97-
.join('\n'),
98-
expected
99-
.split('\n')
100-
.map((x) => x.trimEnd())
101-
.join('\n')
102-
);
103-
})
104-
)
105-
);
106-
});
107-
}
95+
// trim whitespace from line endings,
96+
// this will benefit issues like `form/try-catch-remove` where whitespace is left in the line,
97+
// and testing on windows (\r\n)
98+
expect(
99+
actual
100+
.split('\n')
101+
.map((x) => x.trimEnd())
102+
.join('\n')
103+
).toBe(
104+
expected
105+
.split('\n')
106+
.map((x) => x.trimEnd())
107+
.join('\n')
108+
);
109+
})
110+
)
111+
);
112+
});

packages/commonjs/test/function.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,50 @@
22

33
const { readdirSync } = require('fs');
44

5-
const test = require('ava');
65
const { rollup } = require('rollup');
76

87
const { commonjs, getCodeMapFromBundle, runCodeSplitTest } = require('./helpers/util');
98

10-
process.chdir(__dirname);
9+
const avaAssertions = {
10+
is(actual, expected, message) {
11+
expect(actual, message).toBe(expected);
12+
},
13+
deepEqual(actual, expected, message) {
14+
expect(actual, message).toEqual(expected);
15+
},
16+
truthy(value, message) {
17+
expect(value, message).toBeTruthy();
18+
},
19+
throws(fn, expectation) {
20+
try {
21+
fn();
22+
} catch (error) {
23+
if (expectation?.message instanceof RegExp) {
24+
expect(error.message).toMatch(expectation.message);
25+
} else if (expectation?.message) {
26+
expect(error.message).toBe(expectation.message);
27+
}
28+
29+
return error;
30+
}
31+
32+
return expect.unreachable('Expected function to throw');
33+
}
34+
};
1135

36+
process.chdir(__dirname);
1237
readdirSync('./fixtures/function').forEach((dir) => {
1338
let config;
14-
1539
try {
1640
config = require(`./fixtures/function/${dir}/_config.js`);
1741
} catch (err) {
1842
config = {};
1943
}
20-
2144
if (config.skip) {
2245
console.error(`Skipped test "${dir}"`);
2346
return;
2447
}
25-
(config.solo ? test.only : test)(dir, async (t) => {
48+
(config.solo ? test.only : test)(dir, async () => {
2649
const options = Object.assign(
2750
{
2851
input: `fixtures/function/${dir}/${config.input || 'main.js'}`
@@ -35,7 +58,6 @@ readdirSync('./fixtures/function').forEach((dir) => {
3558
]
3659
}
3760
);
38-
3961
const bundle = await rollup(options);
4062
const codeMap = await getCodeMapFromBundle(bundle, options.output || {});
4163
if (config.show || config.solo) {
@@ -50,16 +72,15 @@ readdirSync('./fixtures/function').forEach((dir) => {
5072
}
5173
const { exports, global, error } = runCodeSplitTest(
5274
codeMap,
53-
t,
75+
avaAssertions,
5476
config.testEntry || 'main.js',
5577
config.context
5678
);
57-
58-
if (config.exports) config.exports(exports, t);
59-
if (config.global) config.global(global, t);
79+
if (config.exports) config.exports(exports, avaAssertions);
80+
if (config.global) config.global(global, avaAssertions);
6081
if (error) {
6182
throw error;
6283
}
63-
t.snapshot(codeMap);
84+
expect(codeMap).toMatchSnapshot();
6485
});
6586
});
226 KB
Binary file not shown.
7.96 KB
Binary file not shown.
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
const path = require('path');
22

3-
const test = require('ava');
43
const { rollup } = require('rollup');
54

65
const { commonjs } = require('./helpers/util.js');
76

87
process.chdir(path.join(__dirname, 'fixtures/samples/dynamic-require-root-outside-cwd/cwd'));
9-
10-
test('crawls dynamicRequireRoot outside cwd', async (t) => {
8+
test('crawls dynamicRequireRoot outside cwd', async () => {
119
const build = await rollup({
1210
input: 'main.js',
1311
plugins: [
@@ -17,7 +15,11 @@ test('crawls dynamicRequireRoot outside cwd', async (t) => {
1715
})
1816
]
1917
});
20-
const bundle = await build.generate({ format: 'cjs' });
18+
const bundle = await build.generate({
19+
format: 'cjs'
20+
});
2121
const { code } = bundle.output[0];
22-
t.true(code.includes('outer_export_value'), 'outer_export_value not found in the code');
22+
expect(code.includes('outer_export_value'), 'outer_export_value not found in the code').toBe(
23+
true
24+
);
2325
});

0 commit comments

Comments
 (0)