Skip to content

Commit fab4c45

Browse files
committed
Support and generate .mjs config files
Fixes #213
1 parent 1889c7c commit fab4c45

File tree

6 files changed

+110
-66
lines changed

6 files changed

+110
-66
lines changed

lib/command.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,16 @@ function resolveReporter(nameOrPath) {
246246

247247
function initJasmine(options) {
248248
const print = options.print;
249-
const specDir = options.specDir;
250-
makeDirStructure(path.join(specDir, 'support/'));
251-
if(!fs.existsSync(path.join(specDir, 'support/jasmine.json'))) {
252-
fs.writeFileSync(path.join(specDir, 'support/jasmine.json'), fs.readFileSync(path.join(__dirname, '../lib/examples/jasmine.json'), 'utf-8'));
253-
}
254-
else {
255-
print('spec/support/jasmine.json already exists in your project.');
249+
const destDir = path.join(options.specDir, 'support/');
250+
makeDirStructure(destDir);
251+
const destPath = path.join(destDir, 'jasmine.mjs');
252+
253+
if (fs.existsSync(destPath)) {
254+
print('spec/support/jasmine.mjs already exists in your project.');
255+
} else {
256+
const contents = fs.readFileSync(
257+
path.join(__dirname, '../lib/examples/jasmine.mjs'), 'utf-8');
258+
fs.writeFileSync(destPath, contents);
256259
}
257260
}
258261

lib/examples/jasmine.json

-14
This file was deleted.

lib/examples/jasmine.mjs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
spec_dir: "spec",
3+
spec_files: [
4+
"**/*[sS]pec.?(m)js"
5+
],
6+
helpers: [
7+
"helpers/**/*.?(m)js"
8+
],
9+
env: {
10+
stopSpecOnExpectationFailure: false,
11+
random: true,
12+
forbidDuplicateNames: true
13+
}
14+
}

lib/runner_base.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,18 @@ class RunnerBase {
102102
} else {
103103
let numFound = 0;
104104

105-
for (const ext of ['json', 'js']) {
105+
for (const ext of ['mjs', 'json', 'js']) {
106106
const candidate = `spec/support/jasmine.${ext}`;
107107

108108
try {
109109
await this.loadSpecificConfigFile_(candidate);
110110
numFound++;
111+
112+
if (ext === 'mjs') {
113+
break;
114+
}
115+
// Otherwise continue possibly loading other files, for backwards
116+
// compatibility.
111117
} catch (e) {
112118
if (e.code !== 'MODULE_NOT_FOUND' // CommonJS
113119
&& e.code !== 'ERR_MODULE_NOT_FOUND' // ESM

spec/command_spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ describe('command', function() {
110110
});
111111

112112
it('creates setup folders and files for specs', function() {
113-
expect(fs.existsSync(path.join(spec, 'support/', 'jasmine.json'))).toBe(true);
113+
expect(fs.existsSync(path.join(spec, 'support/', 'jasmine.mjs'))).toBe(true);
114114
});
115115

116-
it('writes default settings to jasmine.json', function() {
117-
const realJson = fs.readFileSync(path.join(spec, 'support/', 'jasmine.json'), 'utf-8');
118-
const fixtureJson = fs.readFileSync(path.join(__dirname, '../', 'lib/', 'examples/', 'jasmine.json'), 'utf-8');
116+
it('writes default settings to jasmine.mjs', function() {
117+
const realJson = fs.readFileSync(path.join(spec, 'support/', 'jasmine.mjs'), 'utf-8');
118+
const fixtureJson = fs.readFileSync(path.join(__dirname, '../', 'lib/', 'examples/', 'jasmine.mjs'), 'utf-8');
119119
expect(realJson).toEqual(fixtureJson);
120120
});
121121
});

spec/shared_runner_behaviors.js

+75-40
Original file line numberDiff line numberDiff line change
@@ -281,53 +281,88 @@ function sharedRunnerBehaviors(makeRunner) {
281281
await expectAsync(this.fixtureJasmine.loadConfigFile()).toBeResolved();
282282
});
283283

284-
it('loads the default .json configuration file', async function() {
285-
await this.fixtureJasmine.loadConfigFile();
286-
expect(this.fixtureJasmine.specFiles).toEqual([
287-
pathEndingWith('spec/fixtures/sample_project/spec/fixture_spec.js')
288-
]);
289-
});
290-
291-
it('loads the default .js configuration file', async function() {
292-
const config = require('./fixtures/sample_project/spec/support/jasmine.json');
293-
spyOn(Loader.prototype, 'load').and.callFake(function(path) {
294-
if (path.endsWith('jasmine.js')) {
295-
return Promise.resolve(config);
296-
} else {
297-
const e = new Error(`Module not found: ${path}`);
298-
e.code = 'MODULE_NOT_FOUND';
299-
return Promise.reject(e);
300-
}
284+
describe('When the default .mjs configuration file exists', function() {
285+
it('loads the default .mjs configuration file', async function() {
286+
const config = require('./fixtures/sample_project/spec/support/jasmine.json');
287+
spyOn(Loader.prototype, 'load')
288+
.withArgs(jasmine.stringMatching(/jasmine\.mjs$/))
289+
.and.returnValue(Promise.resolve(config));
290+
291+
await this.fixtureJasmine.loadConfigFile();
292+
293+
expect(Loader.prototype.load).toHaveBeenCalledWith(jasmine.stringMatching(
294+
'jasmine\.mjs$'
295+
));
296+
expect(this.fixtureJasmine.specFiles).toEqual([
297+
pathEndingWith('spec/fixtures/sample_project/spec/fixture_spec.js')
298+
]);
301299
});
302300

303-
await this.fixtureJasmine.loadConfigFile();
304-
expect(Loader.prototype.load).toHaveBeenCalledWith(jasmine.stringMatching(
305-
'jasmine\.js$'
306-
));
307-
expect(this.fixtureJasmine.specFiles).toEqual([
308-
pathEndingWith('spec/fixtures/sample_project/spec/fixture_spec.js')
309-
]);
301+
it('does not also load the default .js or .json configuration files', async function() {
302+
spyOn(Loader.prototype, 'load')
303+
.withArgs(jasmine.stringMatching(/jasmine\.mjs$/))
304+
.and.returnValue(Promise.resolve({}));
305+
306+
await this.fixtureJasmine.loadConfigFile();
307+
308+
expect(Loader.prototype.load).not.toHaveBeenCalledWith(jasmine.stringMatching(
309+
'jasmine\.js$'
310+
));
311+
expect(Loader.prototype.load).not.toHaveBeenCalledWith(jasmine.stringMatching(
312+
'jasmine\.json$'
313+
));
314+
});
310315
});
311316

312-
it('warns if both default config files are found', async function() {
313-
spyOn(Loader.prototype, 'load').and.callFake(function (path) {
314-
if (path.endsWith('jasmine.js') || path.endsWith('jasmine.json')) {
315-
return Promise.resolve({});
316-
} else {
317-
const e = new Error(`Module not found: ${path}`);
318-
e.code = 'MODULE_NOT_FOUND';
319-
return Promise.reject(e);
320-
}
317+
describe('When the default .mjs configuration file does not exist', function() {
318+
it('loads the default .json configuration file', async function () {
319+
await this.fixtureJasmine.loadConfigFile();
320+
expect(this.fixtureJasmine.specFiles).toEqual([
321+
pathEndingWith('spec/fixtures/sample_project/spec/fixture_spec.js')
322+
]);
321323
});
322-
spyOn(console, 'warn');
323324

324-
await this.fixtureJasmine.loadConfigFile();
325+
it('loads the default .js configuration file', async function () {
326+
const config = require('./fixtures/sample_project/spec/support/jasmine.json');
327+
spyOn(Loader.prototype, 'load').and.callFake(function (path) {
328+
if (path.endsWith('jasmine.js')) {
329+
return Promise.resolve(config);
330+
} else {
331+
const e = new Error(`Module not found: ${path}`);
332+
e.code = 'MODULE_NOT_FOUND';
333+
return Promise.reject(e);
334+
}
335+
});
336+
337+
await this.fixtureJasmine.loadConfigFile();
338+
expect(Loader.prototype.load).toHaveBeenCalledWith(jasmine.stringMatching(
339+
'jasmine\.js$'
340+
));
341+
expect(this.fixtureJasmine.specFiles).toEqual([
342+
pathEndingWith('spec/fixtures/sample_project/spec/fixture_spec.js')
343+
]);
344+
});
325345

326-
expect(console.warn).toHaveBeenCalledWith(
327-
'Deprecation warning: Jasmine found and loaded both jasmine.js ' +
328-
'and jasmine.json\nconfig files. In a future version, only the ' +
329-
'first file found will be loaded.'
330-
);
346+
it('warns if default .js and .json config files are both found', async function () {
347+
spyOn(Loader.prototype, 'load').and.callFake(function (path) {
348+
if (path.endsWith('jasmine.js') || path.endsWith('jasmine.json')) {
349+
return Promise.resolve({});
350+
} else {
351+
const e = new Error(`Module not found: ${path}`);
352+
e.code = 'MODULE_NOT_FOUND';
353+
return Promise.reject(e);
354+
}
355+
});
356+
spyOn(console, 'warn');
357+
358+
await this.fixtureJasmine.loadConfigFile();
359+
360+
expect(console.warn).toHaveBeenCalledWith(
361+
'Deprecation warning: Jasmine found and loaded both jasmine.js ' +
362+
'and jasmine.json\nconfig files. In a future version, only the ' +
363+
'first file found will be loaded.'
364+
);
365+
});
331366
});
332367
});
333368
});

0 commit comments

Comments
 (0)