Skip to content

Commit

Permalink
Load spec files with the same name in different directories
Browse files Browse the repository at this point in the history
The importer will cache imports with the same name, so we need to make a
fresh copy of the importer with `imports['.']` each time we load a spec
file, in case it has the same name as a preceding spec file.

Closes: #11
  • Loading branch information
ptomato committed Aug 25, 2020
1 parent 2002b5c commit 4723ba9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion jasmine.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"exclude": ["test/fixtures", "test/focusedSpecIntegrationTest.js"],
"exclude": ["test/fixtures/*", "test/focusedSpecIntegrationTest.js"],
"spec_files": "test",
"options": "--verbose"
}
11 changes: 10 additions & 1 deletion src/jasmineBoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,21 @@ var Jasmine = class Jasmine {

loadSpecs() {
const oldSearchPath = imports.searchPath.slice(); // make a copy
let specImporter = imports['.'];
this.specFiles.forEach(function (file) {
const modulePath = GLib.path_get_dirname(file);
const moduleName = GLib.path_get_basename(file).slice(0, -3); // .js

// Backwards compatibility - let specs import modules from their own
// directories
imports.searchPath.unshift(modulePath);
void imports[moduleName];
specImporter.searchPath.unshift(modulePath);
void specImporter[moduleName];
imports.searchPath = oldSearchPath;

// Make a new copy of the importer in case we need to import another
// spec with the same filename, so it is not cached
specImporter = specImporter['.'];
});
}

Expand Down
1 change: 1 addition & 0 deletions test/fixtures/path1/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
describe('A suite', function () {});
1 change: 1 addition & 0 deletions test/fixtures/path2/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
throw new Error('Catch this error to ensure this file is loaded');
33 changes: 30 additions & 3 deletions test/jasmineBootSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,42 @@ describe('Jasmine boot', function () {
expect(testJasmine.specFiles).toEqual([]);
testJasmine.addSpecFiles([`${SRCDIR}test/fixtures`]);
expect(testJasmine.specFiles).toMatchAllFiles([
`${SRCDIR}test/fixtures/someSpec.js`,
`${SRCDIR}test/fixtures/otherSpec.js`,
`${SRCDIR}test/fixtures/path1/test.js`,
`${SRCDIR}test/fixtures/path2/test.js`,
`${SRCDIR}test/fixtures/someSpec.js`,
]);
expect(testJasmine.specFiles.every(path => path.indexOf('notASpec.txt') === -1)).toBe(true);
});

it('adds spec files in different directories with the same name', function () {
testJasmine.addSpecFiles([
`${SRCDIR}test/fixtures/path1`,
`${SRCDIR}test/fixtures/path2`,
]);
expect(testJasmine.specFiles).toMatchAllFiles([
`${SRCDIR}test/fixtures/path1/test.js`,
`${SRCDIR}test/fixtures/path2/test.js`,
]);
});

it('respects excluded files', function () {
testJasmine.exclusions = ['otherSpec.js'];
testJasmine.addSpecFiles([`${SRCDIR}test/fixtures`]);
expect(testJasmine.specFiles).toMatchAllFiles([
`${SRCDIR}test/fixtures/someSpec.js`,
`${SRCDIR}test/fixtures/path1/test.js`,
`${SRCDIR}test/fixtures/path2/test.js`,
]);
});

it('matches when the paths match', function () {
it('matches at the end of the containing path', function () {
testJasmine.exclusions = ['test/fixtures'];
testJasmine.addSpecFiles([`${SRCDIR}test/fixtures`]);
expect(testJasmine.specFiles).toMatchAllFiles([]);
expect(testJasmine.specFiles).toMatchAllFiles([
`${SRCDIR}test/fixtures/path1/test.js`,
`${SRCDIR}test/fixtures/path2/test.js`,
]);
});

it('can handle globs in excluded files', function () {
Expand All @@ -144,4 +162,13 @@ describe('Jasmine boot', function () {
testJasmine.addReporter(fakeReporter);
expect(fakeReporter.jasmine_core_path).toMatch('fake/jasmine/path');
});

it('imports spec files in different directories with the same name', function () {
testJasmine.addSpecFiles([
`${SRCDIR}test/fixtures/path1`,
`${SRCDIR}test/fixtures/path2`,
]);
expect(() => testJasmine.loadSpecs()).toThrowError(Error,
'Catch this error to ensure this file is loaded');
});
});

0 comments on commit 4723ba9

Please sign in to comment.