Skip to content

Commit

Permalink
feat: ✨ support testing esm only pkg (#11684)
Browse files Browse the repository at this point in the history
Co-authored-by: pshu <[email protected]>
  • Loading branch information
stormslowly and stormslowly authored Sep 26, 2023
1 parent b4b3c6e commit 76b5943
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/test-test/esm_only_pkg/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export default "in_esm_only_pkg";

6 changes: 6 additions & 0 deletions examples/test-test/esm_only_pkg/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "esm_only_pkg",
"version": "1.0.0",
"module": "main.js",
"dependencies": {}
}
1 change: 1 addition & 0 deletions examples/test-test/foo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test('class', () => {
return 'foo';
}
}

const a = new A();
expect(a.foo()).toEqual('foo');
});
6 changes: 6 additions & 0 deletions examples/test-test/importEsmOnlyPackage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @ts-ignore
import x from './esm_only_pkg';

test('import from esm only pkg', () => {
expect(x).toEqual('in_esm_only_pkg');
});
1 change: 1 addition & 0 deletions packages/testing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export function createConfig(opts?: {
'<rootDir>/packages/.+/fixtures',
],
setupFiles: [require.resolve('../setupFiles/shim')],
resolver: require.resolve('./resolver.js'),
};
if (opts?.target === 'browser') {
config.testEnvironment = 'jsdom';
Expand Down
17 changes: 17 additions & 0 deletions packages/testing/src/resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* reference: https://jestjs.io/docs/configuration#resolver-string
*
* */
module.exports = (path, options) => {
// Call the defaultResolver, so we leverage its cache, error handling, etc.
return options.defaultResolver(path, {
...options,
// Use packageFilter to process parsed `package.json` before the resolution (see https://www.npmjs.com/package/resolve#resolveid-opts-cb)
packageFilter: pkg => {
return {
...pkg,
main: pkg.main || pkg.module,
};
},
});
};

0 comments on commit 76b5943

Please sign in to comment.