-
Notifications
You must be signed in to change notification settings - Fork 40
/
integration-node-smoke.test.ts
129 lines (106 loc) · 4.28 KB
/
integration-node-smoke.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* eslint-disable jest/require-hook */
/* eslint-disable jest/no-conditional-in-test, jest/no-conditional-expect */
// * These are tests that ensure babel-plugin-tester works (1) with the babel
// * versions we claim it does, (2) with the test frameworks we claim it does,
// * (3) with the feature set we claim and interoperability code given in the
// * documentation.
import debugFactory from 'debug';
import mergeWith from 'lodash.mergewith';
import { existsSync } from 'node:fs';
import { exports as pkgExports, name as pkgName } from '../../package.json';
import { withMockedFixture } from '../setup';
import { assets } from './assets';
import {
BABEL_VERSIONS_UNDER_TEST,
FRAMEWORKS_UNDER_TEST,
defaultFixtureOptions,
type IMPORT_SPECIFIERS_UNDER_TEST
} from './test-config';
const TEST_IDENTIFIER = 'node-smoke';
const TEST_TARGET: (typeof IMPORT_SPECIFIERS_UNDER_TEST)[number] = 'main'; // * Or: 'pure'
const debug = debugFactory(`${pkgName}:${TEST_IDENTIFIER}`);
const pkgMainPath = `${__dirname}/../../${pkgExports['.'].default}`;
const pkgPurePath = `${__dirname}/../../${pkgExports['./pure'].default}`;
debug('FRAMEWORKS_UNDER_TEST: %O', FRAMEWORKS_UNDER_TEST);
debug('BABEL_VERSIONS_UNDER_TEST: %O', BABEL_VERSIONS_UNDER_TEST);
beforeAll(async () => {
if (!existsSync(pkgMainPath)) {
debug(`unable to find main export: ${pkgMainPath}`);
throw new Error('must build distributables first (try `npm run build-dist`)');
}
if (!existsSync(pkgPurePath)) {
debug(`unable to find pure export: ${pkgPurePath}`);
throw new Error('must build distributables first (try `npm run build-dist`)');
}
});
let counter = 1;
for (const [babelPkg, ...otherBabelPkgs] of BABEL_VERSIONS_UNDER_TEST) {
for (const {
frameworkPkg,
frameworkArgs,
otherFrameworkPkgs,
tests
} of FRAMEWORKS_UNDER_TEST) {
const otherPkgs = otherBabelPkgs.concat(otherFrameworkPkgs || []);
const pkgsString = [babelPkg, frameworkPkg, ...otherPkgs].join(', ');
for (const [index, { source, expectations }] of tests.entries()) {
const count = counter++;
const title = `${count}. works with ${pkgsString} [ subtest #${index + 1} ]`;
debug(`registered test: ${title}`);
// eslint-disable-next-line jest/valid-title
(process.env.NO_CONCURRENT ? it : it.concurrent)(title, async () => {
// eslint-disable-next-line jest/no-standalone-expect
expect.hasAssertions();
debug(`started running test: ${title}`);
const indexPath = 'src/index.test.js';
const importSpecifier = `${pkgName}${TEST_TARGET === 'main' ? '' : '/pure'}`;
const fixtureOptions = mergeWith(
{},
defaultFixtureOptions,
{
npmInstall: [frameworkPkg, babelPkg, ...otherPkgs].filter(
(p) => !p.startsWith('node:')
),
runWith: {
binary: 'npx',
args: [...frameworkArgs],
options: {
env: { NODE_OPTIONS: '--no-warnings --experimental-vm-modules' }
}
}
},
{
initialFileContents: {
'fixtures/dummy-fixture-asset/code.js':
assets.dummyFixtureAssetCode[TEST_TARGET],
'fixtures/dummy-fixture-asset/options.js':
assets.dummyFixtureAssetOptions[TEST_TARGET],
'fixtures/dummy-fixture-asset/output.js':
assets.dummyFixtureAssetOutput[TEST_TARGET]
}
}
);
const sourceInput = source[TEST_TARGET];
const sourceCode =
typeof sourceInput === 'string' ? sourceInput : sourceInput['cjs'];
fixtureOptions.initialFileContents[indexPath] = `
const { pluginTester } = require('${importSpecifier}');
const identifierReversePlugin = require('../plugin-identifier-reverse.js');
${sourceCode}
`;
await withMockedFixture({
testIdentifier: TEST_IDENTIFIER,
options: fixtureOptions,
fn: async (context) => {
if (!context.testResult) {
throw new Error('must use node-import-test fixture');
}
expectations(context);
}
});
});
}
}
}
debug('finished registering tests');
debug(`registered a total of ${counter} tests!`);