Skip to content

Instantly share code, notes, and snippets.

@jbenner-radham
Last active December 16, 2021 19:25
Show Gist options
  • Save jbenner-radham/d39c254a6822155e09f6f9a51da27d40 to your computer and use it in GitHub Desktop.
Save jbenner-radham/d39c254a6822155e09f6f9a51da27d40 to your computer and use it in GitHub Desktop.
Using Jest with ES Modules (Without Babel)
export default () => ({});
import { fileURLToPath } from 'url';
import lib from '../';
// The `__dirname` psuedo-global is not available when using es-modules.
// The following can be used as a replacement.
const dirname = fileURLToPath(new URL('./', import.meta.url));
describe('lib', () => {
it('is a function', () => {
expect(lib).toBeFunction();
});
});
module.exports = {
setupFilesAfterEnv: ['jest-extended'],
transform: {} // <-- Important because it instructs Jest not to perform any transformations on code
};
{
"name": "jest-with-es-modules",
"type": "module",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "NODE_OPTIONS=--experimental-vm-modules jest --watchAll --verbose"
},
"devDependencies": {
"jest": "^27.4.5",
"jest-extended": "^1.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment