-
Notifications
You must be signed in to change notification settings - Fork 250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supporting native ES imports for jasmine and mocha #2922
Comments
I've taken a better look at the VM Module class. It is interesting stuff and might work, but it is still very experimental. I don't want to rely on an experimental feature. Using a VM module will also not allow users to use a custom loader:
|
We've discussed it during the community meeting today. We've currently decided to: We can also try to find out if we can discover projects that use ESM, so we can warn users that use the "run" setting. Simply looking in the files for |
With regards to I've been thinking about this a lot lately. Some pain could be taken away if we simply knew whether a file is executed as an ESM or simple (cjs) script. This could eliminate the requirement for Any static code analysis is pointless, as almost everyone uses a code bundler or transpiler. See https://stackoverflow.com/questions/68631696/how-to-know-whether-a-piece-of-js-is-executed-in-an-es-module-or-a-regular-scrip/ (thanks @JohnGorter 😘) We could add some instrumentation atop each file: __stryker__.files = __stryker__.files || {};
__stryker__.files['current/file/name.ts'] = {
esm: this === undefined
} And require the test runner plugin to provide this information to the core StrykerJS process. That way we would know exactly which file is an ESM and which is a regular script. There is one more point. Both Jest and Karma have the ability to run static mutants without having to restart the process. So there should be some way for test runners to communicate this to the core StrykerJS process. I think the simplest way would be to add an optional interface TestRunner {
capabilities?: TestRunnerCapabilities;
}
interface TestRunnerCapabilities {
runStaticMutants?: boolean;
} |
Having a single boolean would be better as the user wouldn't have to have that much knowledge about what static mutants are. As discussed in person:
|
Note: running a |
Note: work on |
I don't feel comfortable in our abilities to "discover" the module type during dry-run. That's why I propose adding an option setting for it. I think it would be elegant to keep it in sync with nodejs and the browser: {
"type": "module"
} The default value can be discovered from the See https://nodejs.org/dist/latest/docs/api/packages.html#type |
An interesting development. Apparently, even if your project is 100% cjs, it might still be imported in the import cache. Removing it from See this example: // a.cjs
module.exports.a = 'a';
console.log('a.cjs loaded');
// b.mjs
export const b = 'b';
console.log('b.mjs loaded');
// entry.cjs
async function importAB() {
const [{ a }, { b }] = await Promise.all([import('./a.cjs'), import('./b.mjs')]);
console.log('Imported: ', a, b);
}
function requireA(){
const { a } = require('./a.cjs');
console.log('Required', a);
}
async function main() {
await importAB();
requireA();
delete require.cache[require.resolve('./a.cjs')];
await importAB();
requireA();
}
main().catch(console.error); Running
As you can see, the second It actually makes sense when you think about it, but I assumed the import cache would be rightly coupled to the This means that the This is the case in the recently released Jasmine 4, which is the reason it isn't working, see #3340 I'm unsure how to proceed. It might be simpler (and easier) to stop clearing files from cache altogether. |
I've discussed it with @simondel and we've decided to do exactly that. Not clearing files from cache altogether. This means that mocha and jasmine test workers will have to restart between runs for static mutants. |
Closed with #3396 |
Is your feature request related to a problem? Please describe.
Native ECMAScript modules or ESM for short, are a new way to import files using the
import
andexport
keywords. NodeJS 12+ supports this out of the box, see https://nodejs.org/api/esm.html for more info. Contrary to CommonJS modules, or CJS for short, which is the way nodejs has always worked (module.exports
andrequire
). Both Mocha and Jasmine support ESM.However, although Mocha and Jasmine support them, our
@stryker-mutator/mocha-runner
and@stryker-mutator/jasmine-runner
plugins still need to clear files from esm cache between runs. For CJS, this is done using the DirectoryRequireCache helper class. However, clearing files from cache is a feature that is not supported with EMS. Since reloading the files is important for both Mocha and Jasmine, to support ESM, you will currently have to run with--maxTestRunnerReuse
set to1
, so a fresh process is created for each test run.There are 2 reasons why all files currently always need to reload between runs:
--coverageAnalysis
is not"off"
Describe the solution you'd like
I can think of a couple of features to improve ESM support:
Ignored
). This is helpful for performance as well, since static mutants are a disaster for performance, especially with large test sets (we run all tests for them by default).Describe alternatives you've considered
Additional context
I don't think the Jest and karma test runner plugins currently have issues with ESM. Jest "supports" them by simply transpiling the import to (old) CJS syntax. Karma reloads the page between runs, which will automatically clear the internal ESM cache,
The text was updated successfully, but these errors were encountered: