Skip to content

Commit b93ca07

Browse files
committed
fix: address vitest phase 4 review follow-ups
1 parent 62cbea0 commit b93ca07

28 files changed

Lines changed: 1931 additions & 2279 deletions

packages/commonjs/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
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 -- --reporter=verbose",
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",

packages/commonjs/test/form.js

Lines changed: 81 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ const acorn = require('acorn');
77

88
const { commonjs } = require('./helpers/util.js');
99

10-
const { createAvaAssertions } = require('./helpers/ava-assertions.js');
11-
12-
const t = createAvaAssertions();
13-
1410
process.chdir(__dirname);
15-
1611
const transformContext = {
1712
error: (base, props) => {
1813
let error = base;
1914
if (!(base instanceof Error)) error = Object.assign(new Error(base.message), base);
2015
if (props) Object.assign(error, props);
2116
throw error;
2217
},
23-
load: ({ id }) => Promise.resolve({ id, meta: {} }),
18+
load: ({ id }) =>
19+
Promise.resolve({
20+
id,
21+
meta: {}
22+
}),
2423
parse: (input, options) =>
2524
acorn.parse(input, {
2625
ecmaVersion: 9,
@@ -33,78 +32,81 @@ const transformContext = {
3332
})
3433
};
3534

36-
// Do not run on Windows as we have full path names in the output
37-
if (path.sep === '/') {
38-
fs.readdirSync('./fixtures/form').forEach((dir) => {
39-
let config;
40-
41-
try {
42-
config = require(`./fixtures/form/${dir}/_config.js`);
43-
} catch (err) {
44-
config = {};
45-
}
46-
47-
const inputEntries = [];
48-
49-
if (typeof config.multi === 'object') {
50-
for (const [key, entry] of Object.entries(config.multi)) {
51-
inputEntries.push([key, `fixtures/form/${dir}/${entry}`]);
52-
}
53-
} else {
54-
inputEntries.push(['output', `fixtures/form/${dir}/input.js`]);
35+
const runFormTest = test.skipIf(path.sep !== '/');
36+
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}`]);
5548
}
56-
57-
(config.solo ? test.only : test)(dir, () =>
58-
Promise.all(
59-
inputEntries.map(async ([outputName, id]) => {
60-
const { buildStart, transform } = commonjs(config.options);
61-
buildStart.call({ meta: { rollupVersion: '99.0.0' } }, { plugins: [] });
62-
transformContext.getModuleInfo = (moduleId) => {
63-
return {
64-
isEntry: config.entry && moduleId === id,
65-
importers:
66-
config.importers && config.importers[outputName]
67-
? config.importers[outputName].map((x) => `fixtures/form/${dir}/${x}`)
68-
: [],
69-
meta: {}
70-
};
71-
};
72-
const input = fs.readFileSync(id, 'utf-8');
73-
74-
let outputFile = `fixtures/form/${dir}/${outputName}`;
75-
if (fs.existsSync(`${outputFile}.${process.platform}.js`)) {
76-
outputFile += `.${process.platform}.js`;
77-
} else {
78-
outputFile += '.js';
79-
}
80-
81-
const expected = fs.readFileSync(outputFile, 'utf-8').trim();
82-
// eslint-disable-next-line no-await-in-loop
83-
const transformed = await transform.call(transformContext, input, id);
84-
let actual = (transformed ? transformed.code : input).trim().replace(/\0/g, '_');
85-
const cwd = process.cwd();
86-
while (actual.indexOf(cwd) >= 0) {
87-
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: []
8864
}
89-
90-
// uncomment to update snapshots
91-
// fs.writeFileSync(outputFile, `${actual}\n`);
92-
93-
// trim whitespace from line endings,
94-
// this will benefit issues like `form/try-catch-remove` where whitespace is left in the line,
95-
// and testing on windows (\r\n)
96-
t.is(
97-
actual
98-
.split('\n')
99-
.map((x) => x.trimEnd())
100-
.join('\n'),
101-
expected
102-
.split('\n')
103-
.map((x) => x.trimEnd())
104-
.join('\n')
105-
);
106-
})
107-
)
108-
);
109-
});
110-
}
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+
}
91+
92+
// uncomment to update snapshots
93+
// fs.writeFileSync(outputFile, `${actual}\n`);
94+
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: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,41 @@ const { rollup } = require('rollup');
66

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

9-
const { createAvaAssertions } = require('./helpers/ava-assertions.js');
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+
}
1028

11-
const t = createAvaAssertions();
29+
return error;
30+
}
1231

13-
process.chdir(__dirname);
32+
return expect.unreachable('Expected function to throw');
33+
}
34+
};
1435

36+
process.chdir(__dirname);
1537
readdirSync('./fixtures/function').forEach((dir) => {
1638
let config;
17-
1839
try {
1940
config = require(`./fixtures/function/${dir}/_config.js`);
2041
} catch (err) {
2142
config = {};
2243
}
23-
2444
if (config.skip) {
2545
console.error(`Skipped test "${dir}"`);
2646
return;
@@ -38,7 +58,6 @@ readdirSync('./fixtures/function').forEach((dir) => {
3858
]
3959
}
4060
);
41-
4261
const bundle = await rollup(options);
4362
const codeMap = await getCodeMapFromBundle(bundle, options.output || {});
4463
if (config.show || config.solo) {
@@ -53,16 +72,15 @@ readdirSync('./fixtures/function').forEach((dir) => {
5372
}
5473
const { exports, global, error } = runCodeSplitTest(
5574
codeMap,
56-
t,
75+
avaAssertions,
5776
config.testEntry || 'main.js',
5877
config.context
5978
);
60-
61-
if (config.exports) config.exports(exports, t);
62-
if (config.global) config.global(global, t);
79+
if (config.exports) config.exports(exports, avaAssertions);
80+
if (config.global) config.global(global, avaAssertions);
6381
if (error) {
6482
throw error;
6583
}
66-
t.snapshot(codeMap);
84+
expect(codeMap).toMatchSnapshot();
6785
});
6886
});

0 commit comments

Comments
 (0)