|
3 | 3 |
|
4 | 4 | // * These are tests that ensure babel-plugin-tester works (1) in ESM vs CJS |
5 | 5 | // * environments, (2) using modern vs modern-default vs default vs dot-default |
6 | | -// * import syntax, (3) using main vs pure import specifiers. |
| 6 | +// * import syntax, (3) using main vs pure import specifiers, (4) across all |
| 7 | +// * maintained versions of NodeJS. |
7 | 8 |
|
8 | 9 | import { existsSync } from 'node:fs'; |
9 | 10 | import debugFactory from 'debug'; |
10 | 11 | import mergeWith from 'lodash.mergewith'; |
| 12 | +import path from 'node:path'; |
11 | 13 |
|
12 | 14 | import { name as pkgName, exports as pkgExports } from '../../package.json'; |
13 | 15 | import { withMockedFixture } from '../setup'; |
14 | 16 | import { assets } from './assets'; |
| 17 | +import { expectSuccessAndOutput } from './test-expectations'; |
15 | 18 |
|
16 | 19 | import { |
17 | 20 | defaultFixtureOptions, |
18 | 21 | BABEL_VERSIONS_UNDER_TEST, |
19 | 22 | IMPORT_SPECIFIERS_UNDER_TEST, |
20 | | - IMPORT_STYLES_UNDER_TEST |
| 23 | + IMPORT_STYLES_UNDER_TEST, |
| 24 | + NODE_VERSIONS_UNDER_TEST |
21 | 25 | } from './test-config'; |
22 | | -import { expectSuccessAndOutput } from './test-expectations'; |
23 | | -import { withNodeTestInterop } from './test-interop'; |
24 | 26 |
|
25 | 27 | const TEST_IDENTIFIER = 'node-interop'; |
26 | 28 | const debug = debugFactory(`${pkgName}:${TEST_IDENTIFIER}`); |
@@ -49,93 +51,104 @@ let counter = 1; |
49 | 51 | for (const esm of [true, false] as const) { |
50 | 52 | for (const importSpecifierName of IMPORT_SPECIFIERS_UNDER_TEST) { |
51 | 53 | for (const importStyleName of IMPORT_STYLES_UNDER_TEST) { |
52 | | - const count = counter++; |
53 | | - const title = `${count}. works as a ${importStyleName} ${importSpecifierName} ${ |
54 | | - esm ? 'ESM' : 'CJS' |
55 | | - } import`; |
56 | | - |
57 | | - if (esm && importStyleName == 'dot-default') { |
58 | | - debug(`skipped test due to incompatible options: ${title}`); |
59 | | - continue; |
60 | | - } |
61 | | - |
62 | | - debug(`registered test: ${title}`); |
63 | | - |
64 | | - // eslint-disable-next-line jest/valid-title |
65 | | - (process.env.NO_CONCURRENT ? it : it.concurrent)(title, async () => { |
66 | | - // eslint-disable-next-line jest/no-standalone-expect |
67 | | - expect.hasAssertions(); |
68 | | - |
69 | | - debug(`started running test: ${title}`); |
70 | | - |
71 | | - const indexPath = `src/index.test.${esm ? 'm' : ''}js`; |
72 | | - const importSpecifier = `${pkgName}${ |
73 | | - importSpecifierName == 'main' ? '' : '/pure' |
74 | | - }`; |
75 | | - |
76 | | - const importStyle = { |
77 | | - modern: '{ pluginTester }', |
78 | | - 'modern-default': '{ default: pluginTester }', |
79 | | - default: 'pluginTester', |
80 | | - 'dot-default': 'pluginTester' |
81 | | - }[importStyleName]; |
82 | | - |
83 | | - const fixtureOptions = mergeWith( |
84 | | - {}, |
85 | | - defaultFixtureOptions, |
86 | | - { |
87 | | - npmInstall: ['@babel/core@latest'], |
88 | | - runWith: { |
89 | | - binary: 'npx', |
90 | | - args: ['node'] |
| 54 | + for (const nodeVersion of NODE_VERSIONS_UNDER_TEST) { |
| 55 | + const count = counter++; |
| 56 | + const title = `${count}. works as a ${importStyleName} ${importSpecifierName} ${ |
| 57 | + esm ? 'ESM' : 'CJS' |
| 58 | + } import using ${nodeVersion}`; |
| 59 | + |
| 60 | + if (esm && importStyleName == 'dot-default') { |
| 61 | + debug(`skipped test due to incompatible options: ${title}`); |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + debug(`registered test: ${title}`); |
| 66 | + |
| 67 | + // eslint-disable-next-line jest/valid-title |
| 68 | + (process.env.NO_CONCURRENT ? it : it.concurrent)(title, async () => { |
| 69 | + // eslint-disable-next-line jest/no-standalone-expect |
| 70 | + expect.hasAssertions(); |
| 71 | + |
| 72 | + debug(`started running test: ${title}`); |
| 73 | + |
| 74 | + const indexPath = `src/index.test.${esm ? 'm' : ''}js`; |
| 75 | + const importSpecifier = `${pkgName}${ |
| 76 | + importSpecifierName == 'main' ? '' : '/pure' |
| 77 | + }`; |
| 78 | + |
| 79 | + const importStyle = { |
| 80 | + modern: '{ pluginTester }', |
| 81 | + 'modern-default': '{ default: pluginTester }', |
| 82 | + default: 'pluginTester', |
| 83 | + 'dot-default': 'pluginTester' |
| 84 | + }[importStyleName]; |
| 85 | + |
| 86 | + const fixtureOptions = mergeWith( |
| 87 | + {}, |
| 88 | + defaultFixtureOptions, |
| 89 | + { |
| 90 | + performCleanup: false, |
| 91 | + runInstallScripts: true, |
| 92 | + npmInstall: ['@babel/core@latest', 'jest@latest', nodeVersion], |
| 93 | + runWith: { |
| 94 | + binary: path.join('node_modules', '.bin', 'node'), |
| 95 | + args: [ |
| 96 | + '--experimental-vm-modules', |
| 97 | + path.join('node_modules', '.bin', 'jest') |
| 98 | + ] |
| 99 | + } |
| 100 | + }, |
| 101 | + { |
| 102 | + initialFileContents: { |
| 103 | + 'jest.config.js': |
| 104 | + 'module.exports = {testMatch:["**/?(*.)+(spec|test).?(m)[jt]s?(x)"],transform:{}};', |
| 105 | + 'fixtures/dummy-fixture-asset/code.js': |
| 106 | + assets.dummyFixtureAssetCode[importSpecifierName], |
| 107 | + 'fixtures/dummy-fixture-asset/options.js': |
| 108 | + assets.dummyFixtureAssetOptions[importSpecifierName], |
| 109 | + 'fixtures/dummy-fixture-asset/output.js': |
| 110 | + assets.dummyFixtureAssetOutput[importSpecifierName] |
| 111 | + } |
91 | 112 | } |
92 | | - }, |
93 | | - { |
94 | | - initialFileContents: { |
95 | | - 'fixtures/dummy-fixture-asset/code.js': |
96 | | - assets.dummyFixtureAssetCode[importSpecifierName], |
97 | | - 'fixtures/dummy-fixture-asset/options.js': |
98 | | - assets.dummyFixtureAssetOptions[importSpecifierName], |
99 | | - 'fixtures/dummy-fixture-asset/output.js': |
100 | | - assets.dummyFixtureAssetOutput[importSpecifierName] |
101 | | - } |
102 | | - } |
103 | | - ); |
| 113 | + ); |
104 | 114 |
|
105 | | - const sourceInput = withNodeTestInterop(assets.invocation)[importSpecifierName]; |
106 | | - const sourceCode = |
107 | | - typeof sourceInput == 'string' ? sourceInput : sourceInput[esm ? 'esm' : 'cjs']; |
| 115 | + const sourceInput = assets.invocation[importSpecifierName]; |
| 116 | + const sourceCode = |
| 117 | + typeof sourceInput == 'string' |
| 118 | + ? sourceInput |
| 119 | + : sourceInput[esm ? 'esm' : 'cjs']; |
108 | 120 |
|
109 | | - fixtureOptions.initialFileContents[indexPath] = esm |
110 | | - ? ` |
| 121 | + fixtureOptions.initialFileContents[indexPath] = esm |
| 122 | + ? ` |
111 | 123 | import ${ |
112 | 124 | esm ? importStyle.replaceAll(':', ' as') : importStyle |
113 | 125 | } from '${importSpecifier}'; |
114 | 126 | import identifierReversePlugin from '../plugin-identifier-reverse.js'; |
115 | 127 |
|
116 | 128 | ${sourceCode} |
117 | 129 | ` |
118 | | - : ` |
| 130 | + : ` |
119 | 131 | const ${importStyle} = require('${importSpecifier}')${ |
120 | | - importStyleName == 'dot-default' ? '.default' : '' |
121 | | - }; |
| 132 | + importStyleName == 'dot-default' ? '.default' : '' |
| 133 | + }; |
122 | 134 | const identifierReversePlugin = require('../plugin-identifier-reverse.js'); |
123 | 135 |
|
124 | 136 | ${sourceCode} |
125 | 137 | `; |
126 | 138 |
|
127 | | - await withMockedFixture({ |
128 | | - testIdentifier: TEST_IDENTIFIER, |
129 | | - options: fixtureOptions, |
130 | | - fn: async (context) => { |
131 | | - if (!context.testResult) { |
132 | | - throw new Error('must use node-import-test fixture'); |
133 | | - } |
| 139 | + await withMockedFixture({ |
| 140 | + testIdentifier: TEST_IDENTIFIER, |
| 141 | + options: fixtureOptions, |
| 142 | + fn: async (context) => { |
| 143 | + if (!context.testResult) { |
| 144 | + throw new Error('must use node-import-test fixture'); |
| 145 | + } |
134 | 146 |
|
135 | | - expectSuccessAndOutput(context); |
136 | | - } |
| 147 | + expectSuccessAndOutput(context); |
| 148 | + } |
| 149 | + }); |
137 | 150 | }); |
138 | | - }); |
| 151 | + } |
139 | 152 | } |
140 | 153 | } |
141 | 154 | } |
|
0 commit comments