I have a library that I build and maintain. I'm trying to build a particular unit test with it. When I build a class around ClassWeProvide in TypeScript, the Jest unit tests work just fine.
I am trying to test a JS version since some of my library's users may want to use raw JavaScript. My Jest is in TS, and MyJSClass is in JS, intended to be using a "published" version of my library (dist/bundle.js) which is pre-built as part of my pipeline. (I webpack the library into a bundle.)
The issue I'm seeing is that the Jest file, on require is failing to load the JS file do to the require statement below. I cannot figure if I need to configure Jest, or do something entirely different, or maybe this isn't possible.
The library and ClassWeProvide are intended to be run in NodeJS, not the Frontend.
In jest.config.js:
module.exports = {
testEnvironment: 'node',
collectCoverage: false,
coveragePathIgnorePatterns: ['dist', 'other', 'directories'],
roots: ['<rootDir>'],
transform: { '^.+\\.(t|j)s$': 'ts-jest' },
transformIgnorePatterns: ['/node_modules/'],
moduleFileExtensions: ['ts', 'js'],
watchPathIgnorePatterns: ['node_modules'],
moduleNameMapper: {
'my-library': '<rootDir>'
}
};
In my-class.js:
const { ClassWeProvide } = require('my-library');
module.exports = class MyJSClass extends ClassWeProvide {
...
};
In my MyJSClass.test.ts:
/* eslint-disable @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports */
const MyJSClass = require('../MyJSClass');
describe('My JS Class', () => {...});
SyntaxError: Unexpected token ','
> 1 | const { MyJSClass } = require('my-library');