'How to import types in a jestEnvironment?
I'm setting a jest-environment for my tests, and apparently, these environments are not considered as module by Jest.
Therefore, I cannot use the ES6 syntax to import and export.
That is why I am trying to find a way to import interfaces or types using require. Is it possible?
This is my jestEnvironment.ts:
import Config = require('@jest/types');
const NodeEnvironment = require('jest-environment-node').default;
class CustomEnvironment extends NodeEnvironment {
constructor(config: Config.ProjectConfig) {
super(config);
console.log(config);
}
async setup() {
await super.setup();
this.global.someGlobalVar = 'lala';
}
async teardown() {
await super.teardown();
}
getVmContext() {
return super.getVmContext();
}
}
module.exports = CustomEnvironment;
And when running Jest, I get the following error:
● Test suite failed to run
/path/to/project/src/jestEnvironment.ts:1
import { Config } from '@jest/types';
^^^^^^
SyntaxError: Cannot use import statement outside a module
(Just for information: I'm using ts-jest 26.5.6... And I just saw that the version 28 introduced a support to ESM. However, there are too many breaking changes for me to switch to that version for now.)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
