Skip to content

Commit

Permalink
Load specs and helpers using import by default
Browse files Browse the repository at this point in the history
  • Loading branch information
sgravrock committed May 1, 2021
1 parent 6c8c6aa commit a330ac9
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 23 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
spec/fixtures/cjs-syntax-error/syntax_error.js
spec/fixtures/js-loader-import/*.js
spec/fixtures/js-loader-default/*.js
19 changes: 7 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,13 @@ jasmine JASMINE_CONFIG_PATH=relative/path/to/your/jasmine.json
jasmine --config=relative/path/to/your/jasmine.json
```

## Using ES modules

If the name of a spec file or helper file ends in `.mjs`, Jasmine will load it
as an [ES module](https://nodejs.org/docs/latest-v13.x/api/esm.html) rather
than a CommonJS module. This allows the spec file or helper to import other
ES modules. No extra configuration is required.

You can also use ES modules with names ending in `.js` by adding
`"jsLoader": "import"` to `jasmine.json`. This should work for CommonJS modules
as well as ES modules. We expect to make it the default in a future release.
Please [log an issue](https://github.com/jasmine/jasmine-npm/issues) if you have
code that doesn't load correctly with `"jsLoader": "import"`.
## ES and CommonJS module compatibility

By default, Jasmine uses `import` to load spec files and helper files. This
should work for both ES modules and CommonJS modules. No additional
configuration is required. If you need some files to be loaded via `require`,
add `"jsLoader": "require"` to `jasmine.json`. With that set, Jasmine will use
`require` to load all files with names that don't end in `.mjs`.


# Filtering specs
Expand Down
4 changes: 2 additions & 2 deletions lib/jasmine.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,9 @@ Jasmine.prototype.loadConfig = function(config) {
configuration.random = config.random;
}

if (config.jsLoader === 'import') {
if (config.jsLoader === 'import' || config.jsLoader === undefined) {
this._alwaysImport = true;
} else if (config.jsLoader === 'require' || config.jsLoader === undefined) {
} else if (config.jsLoader === 'require') {
this._alwaysImport = false;
} else {
throw new Error(`"${config.jsLoader}" is not a valid value for the ` +
Expand Down
5 changes: 0 additions & 5 deletions spec/fixtures/js-loader-default/aSpec.js

This file was deleted.

3 changes: 3 additions & 0 deletions spec/fixtures/js-loader-default/anEsModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function foo() {
return 42;
}
7 changes: 7 additions & 0 deletions spec/fixtures/js-loader-default/anEsModuleSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {foo} from './anEsModule.js';

describe('foo', function() {
it('returns 42', function() {
expect(foo()).toEqual(42);
});
});
2 changes: 1 addition & 1 deletion spec/fixtures/js-loader-default/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"spec_dir": ".",
"spec_files": ["aSpec.js"]
"spec_files": ["anEsModuleSpec.js"]
}
3 changes: 3 additions & 0 deletions spec/fixtures/js-loader-default/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
2 changes: 1 addition & 1 deletion spec/integration_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('Integration', function () {
expect(await runJasmine('spec/fixtures/js-loader-require')).toBeSuccess();
});

it('loads .js files using require when jsLoader is undefined', async function() {
it('loads .js files using import when jsLoader is undefined', async function() {
expect(await runJasmine('spec/fixtures/js-loader-default')).toBeSuccess();
});

Expand Down
4 changes: 2 additions & 2 deletions spec/jasmine_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,13 @@ describe('Jasmine', function() {
});

describe('with jsLoader undefined', function () {
it('tells the loader not to always import', async function() {
it('tells the loader to always import', async function() {
this.configObject.jsLoader = undefined;

this.fixtureJasmine.loadConfig(this.configObject);
await this.fixtureJasmine.loadSpecs();

expect(this.loader.load).toHaveBeenCalledWith(jasmine.any(String), false);
expect(this.loader.load).toHaveBeenCalledWith(jasmine.any(String), true);
});
});

Expand Down

0 comments on commit a330ac9

Please sign in to comment.