Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Aug 28, 2023
1 parent 407b1c2 commit d6472cf
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 174 deletions.
11 changes: 5 additions & 6 deletions tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createNode } from './utils/node-with-loader.js';

const nodeVersions = [
'12.16.2', // Pre ESM import
// '12',
'12',
...(
process.env.CI
? [
Expand All @@ -20,16 +20,15 @@ const nodeVersions = [
for (const nodeVersion of nodeVersions) {
const node = await createNode(nodeVersion, './tests/fixtures');

console.log(node);
await describe(`Node ${node.version}`, ({ runTestSuite }) => {
runTestSuite(
import('./specs/javascript/index.js'),
node,
);
// runTestSuite(
// import('./specs/typescript/index.js'),
// node,
// );
runTestSuite(
import('./specs/typescript/index.js'),
node,
);
});
}
})();
315 changes: 157 additions & 158 deletions tests/specs/javascript/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,172 +26,171 @@ export default testSuite(async ({ describe }, node: NodeApis) => {

test('Load', async () => {

Check warning on line 27 in tests/specs/javascript/cjs.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Too many nested callbacks (5). Maximum allowed is 4
const nodeProcess = await node.load(importPath);
console.log(nodeProcess);
assertResults(nodeProcess.stdout);
});

// test('Import', async () => {
// const nodeProcess = await node.importDynamic(importPath);
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('{"default":1234}');
// });
test('Import', async () => {

Check warning on line 32 in tests/specs/javascript/cjs.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Too many nested callbacks (5). Maximum allowed is 4
const nodeProcess = await node.importDynamic(importPath);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});

test('TypeScript Import', async () => {

Check warning on line 38 in tests/specs/javascript/cjs.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Too many nested callbacks (5). Maximum allowed is 4
const nodeProcess = await node.importDynamic(importPath, { mode: 'typescript' });
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});

test('Require', async () => {

Check warning on line 44 in tests/specs/javascript/cjs.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Too many nested callbacks (5). Maximum allowed is 4
const nodeProcess = await node.require(importPath);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('1234');
});
});

describe('extensionless - shoud not work', ({ test }) => {
const importPath = './lib/cjs-ext-cjs/index';

// test('TypeScript Import', async () => {
// const nodeProcess = await node.importDynamic(importPath, { mode: 'typescript' });
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('{"default":1234}');
// });
test('Load', async () => {

Check warning on line 54 in tests/specs/javascript/cjs.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Too many nested callbacks (5). Maximum allowed is 4
const nodeProcess = await node.load(importPath);
expect(nodeProcess.stderr).toMatch('Cannot find module');
});

test('Import', async () => {

Check warning on line 59 in tests/specs/javascript/cjs.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Too many nested callbacks (5). Maximum allowed is 4
const nodeProcess = await node.importDynamic(importPath);
expect(nodeProcess.stderr).toMatch('Cannot find module');
});

// test('Require', async () => {
// const nodeProcess = await node.require(importPath);
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('1234');
// });
test('Require', async () => {

Check warning on line 64 in tests/specs/javascript/cjs.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Too many nested callbacks (5). Maximum allowed is 4
const nodeProcess = await node.require(importPath);
expect(nodeProcess.stderr).toMatch('Cannot find module');
});
});

// describe('extensionless - shoud not work', ({ test }) => {
// const importPath = './lib/cjs-ext-cjs/index';

// test('Load', async () => {
// const nodeProcess = await node.load(importPath);
// expect(nodeProcess.stderr).toMatch('Cannot find module');
// });

// test('Import', async () => {
// const nodeProcess = await node.importDynamic(importPath);
// expect(nodeProcess.stderr).toMatch('Cannot find module');
// });

// test('Require', async () => {
// const nodeProcess = await node.require(importPath);
// expect(nodeProcess.stderr).toMatch('Cannot find module');
// });
// });

// describe('directory - shoud not work', ({ test }) => {
// const importPath = './lib/cjs-ext-cjs';

// test('Load', async () => {
// const nodeProcess = await node.load(importPath);
// expect(nodeProcess.stderr).toMatch('Cannot find module');
// });

// test('Import', async () => {
// const nodeProcess = await node.importDynamic(importPath);

// if (semver.satisfies(node.version, nodeSupports.import)) {
// expect(nodeProcess.stderr).toMatch('Directory import');
// } else {
// expect(nodeProcess.stderr).toMatch('Cannot find module');
// }
// });

// test('Require', async () => {
// const nodeProcess = await node.require(importPath);
// expect(nodeProcess.stderr).toMatch('Cannot find module');
// });
// });
describe('directory - shoud not work', ({ test }) => {
const importPath = './lib/cjs-ext-cjs';

test('Load', async () => {

Check warning on line 73 in tests/specs/javascript/cjs.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Too many nested callbacks (5). Maximum allowed is 4
const nodeProcess = await node.load(importPath);
expect(nodeProcess.stderr).toMatch('Cannot find module');
});

test('Import', async () => {

Check warning on line 78 in tests/specs/javascript/cjs.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Too many nested callbacks (5). Maximum allowed is 4
const nodeProcess = await node.importDynamic(importPath);

if (semver.satisfies(node.version, nodeSupports.import)) {
expect(nodeProcess.stderr).toMatch('Directory import');
} else {
expect(nodeProcess.stderr).toMatch('Cannot find module');
}
});

test('Require', async () => {
const nodeProcess = await node.require(importPath);
expect(nodeProcess.stderr).toMatch('Cannot find module');
});
});
});

// describe('.js extension', ({ describe }) => {
// function assertResults(stdout: string) {
// expect(stdout).toMatch('loaded cjs-ext-js/index.js');
// expect(stdout).toMatch('✔ has CJS context');
// expect(stdout).toMatch('✔ name in error');
// expect(stdout).toMatch('✔ sourcemaps');
// expect(stdout).toMatch('✔ has dynamic import');
// expect(stdout).toMatch('✔ resolves optional node prefix');
// expect(stdout).toMatch('✔ preserves names');
// expect(stdout).toMatch(
// semver.satisfies(node.version, nodeSupports.testRunner)
// ? '✔ resolves required node prefix'
// : '✖ resolves required node prefix: Error',
// );
// }

// describe('full path', ({ test }) => {
// const importPath = './lib/cjs-ext-js/index.js';

// test('Load', async () => {
// const nodeProcess = await node.load(importPath);
// assertResults(nodeProcess.stdout);
// });

// test('Import', async () => {
// const nodeProcess = await node.importDynamic(importPath);
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('{"default":1234}');
// });

// test('Require', async () => {
// const nodeProcess = await node.require(importPath);
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('1234');
// });
// });

// describe('extensionless', ({ test }) => {
// const importPath = './lib/cjs-ext-js/index';

// test('Load', async () => {
// const nodeProcess = await node.load(importPath);
// assertResults(nodeProcess.stdout);
// });

// /**
// * Dynamic import was introduced in Node v12.20+ and v13.2+
// * https://github.com/evanw/esbuild/blob/783527408b41bf55a6ac7ebb0b1ab4128a29417d/scripts/compat-table.js#L233
// *
// * For Node.js versions that support import, the esm-loader
// * should be used.
// *
// * Alternatively, we can set the target to Node 11
// * and transform the import to a require(). But
// * this means other features will be transpiled.
// */
// test('Import', async () => {
// const nodeProcess = await node.importDynamic(importPath);

// if (semver.satisfies(node.version, nodeSupports.import)) {
// expect(nodeProcess.stderr).toMatch('Cannot find module');
// } else {
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('{"default":1234}');
// }
// });

// test('Require', async () => {
// const nodeProcess = await node.require(importPath);
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('1234');
// });
// });

// describe('directory', ({ test }) => {
// const importPath = './lib/cjs-ext-js';

// test('Load', async () => {
// const nodeProcess = await node.load(importPath);
// assertResults(nodeProcess.stdout);
// });

// test('Import', async () => {
// const nodeProcess = await node.importDynamic(importPath);

// if (semver.satisfies(node.version, nodeSupports.import)) {
// expect(nodeProcess.stderr).toMatch('Directory import');
// } else {
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('{"default":1234}');
// }
// });

// test('Require', async () => {
// const nodeProcess = await node.require(importPath);
// assertResults(nodeProcess.stdout);
// expect(nodeProcess.stdout).toMatch('1234');
// });
// });
// });
describe('.js extension', ({ describe }) => {
function assertResults(stdout: string) {
expect(stdout).toMatch('loaded cjs-ext-js/index.js');
expect(stdout).toMatch('✔ has CJS context');
expect(stdout).toMatch('✔ name in error');
expect(stdout).toMatch('✔ sourcemaps');
expect(stdout).toMatch('✔ has dynamic import');
expect(stdout).toMatch('✔ resolves optional node prefix');
expect(stdout).toMatch('✔ preserves names');
expect(stdout).toMatch(
semver.satisfies(node.version, nodeSupports.testRunner)
? '✔ resolves required node prefix'
: '✖ resolves required node prefix: Error',
);
}

describe('full path', ({ test }) => {
const importPath = './lib/cjs-ext-js/index.js';

test('Load', async () => {
const nodeProcess = await node.load(importPath);
assertResults(nodeProcess.stdout);
});

test('Import', async () => {
const nodeProcess = await node.importDynamic(importPath);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
});

test('Require', async () => {
const nodeProcess = await node.require(importPath);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('1234');
});
});

describe('extensionless', ({ test }) => {
const importPath = './lib/cjs-ext-js/index';

test('Load', async () => {
const nodeProcess = await node.load(importPath);
assertResults(nodeProcess.stdout);
});

/**
* Dynamic import was introduced in Node v12.20+ and v13.2+
* https://github.com/evanw/esbuild/blob/783527408b41bf55a6ac7ebb0b1ab4128a29417d/scripts/compat-table.js#L233
*
* For Node.js versions that support import, the esm-loader
* should be used.
*
* Alternatively, we can set the target to Node 11
* and transform the import to a require(). But
* this means other features will be transpiled.
*/
test('Import', async () => {
const nodeProcess = await node.importDynamic(importPath);

if (semver.satisfies(node.version, nodeSupports.import)) {
expect(nodeProcess.stderr).toMatch('Cannot find module');
} else {
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
}
});

test('Require', async () => {
const nodeProcess = await node.require(importPath);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('1234');
});
});

describe('directory', ({ test }) => {
const importPath = './lib/cjs-ext-js';

test('Load', async () => {
const nodeProcess = await node.load(importPath);
assertResults(nodeProcess.stdout);
});

test('Import', async () => {
const nodeProcess = await node.importDynamic(importPath);

if (semver.satisfies(node.version, nodeSupports.import)) {
expect(nodeProcess.stderr).toMatch('Directory import');
} else {
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('{"default":1234}');
}
});

test('Require', async () => {
const nodeProcess = await node.require(importPath);
assertResults(nodeProcess.stdout);
expect(nodeProcess.stdout).toMatch('1234');
});
});
});
});
});
16 changes: 8 additions & 8 deletions tests/specs/javascript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
import('./cjs.js'),
node,
);
// runTestSuite(
// import('./esm.js'),
// node,
// );
// runTestSuite(
// import('./dependencies.js'),
// node,
// );
runTestSuite(
import('./esm.js'),
node,
);
runTestSuite(
import('./dependencies.js'),
node,
);
});
});
2 changes: 0 additions & 2 deletions tests/utils/node-with-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export async function createNode(
arch: 'x64',
});

console.log('downloaded', node);

return {
version: node.version,
load(
Expand Down

0 comments on commit d6472cf

Please sign in to comment.