Skip to content

Commit

Permalink
Add testModules to virtual content
Browse files Browse the repository at this point in the history
  • Loading branch information
lolmaus committed Jan 15, 2024
1 parent db809ec commit 2a6a486
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 17 deletions.
18 changes: 7 additions & 11 deletions packages/compat/src/compat-app-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@ export class CompatAppBuilder {
let amdModules = nonFastboot.map(file => this.importPaths(appFiles, file));
let fastbootOnlyAmdModules = fastboot.map(file => this.importPaths(appFiles, file));

let testModules: { runtime: string; buildtime: string }[] = [];

for (let relativePath of appFiles.tests) {
testModules.push(this.importPaths(appFiles, relativePath));
}

let config: CompatResolverOptions = {
// this part is the base ModuleResolverOptions as required by @embroider/core
renameModules,
Expand Down Expand Up @@ -336,6 +342,7 @@ export class CompatAppBuilder {
options,
amdModules,
fastbootOnlyAmdModules,
testModules,
};

return config;
Expand Down Expand Up @@ -1366,11 +1373,6 @@ export class CompatAppBuilder {
return asset;
}

// We're only building tests from the first engine (the app). This is the
// normal thing to do -- tests from engines don't automatically roll up into
// the app.
let engine = appFiles[0];

const myName = 'assets/test.js';

// tests necessarily also include the app. This is where we account for
Expand All @@ -1383,12 +1385,6 @@ export class CompatAppBuilder {
explicitRelative(dirname(myName), this.topAppJSAsset(appFiles, prepared).relativePath),
];

let amdModules: { runtime: string; buildtime: string }[] = [];

for (let relativePath of engine.tests) {
amdModules.push(this.importPaths(engine, relativePath));
}

let source = entryTemplate({
eagerModules,
testSuffix: true,
Expand Down
1 change: 1 addition & 0 deletions packages/compat/tests/audit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ describe('audit', function () {
resolvableExtensions,
amdModules: [],
fastbootOnlyAmdModules: [],
testModules: [],
};

let babel: TransformOptions = {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface Options {
amdCompatibility: Required<UserOptions['amdCompatibility']>;
amdModules: AmdModule[];
fastbootOnlyAmdModules: AmdModule[];
testModules: AmdModule[];
}

export interface AmdModule {
Expand Down
10 changes: 8 additions & 2 deletions packages/core/src/virtual-content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,17 @@ function renderImplicitModules(
}

function renderAmdModules(
_amdModulesPlaceholderModule: { type: 'amd-modules' | 'amd-test-modules'; fromFile: string },
{ type }: { type: 'amd-modules' | 'amd-test-modules'; fromFile: string },
resolver: Resolver
): string {
const amdModules = [...resolver.options.amdModules];

if (type === 'amd-test-modules') {
amdModules.push(...resolver.options.testModules);
}

return amdModulesTemplate({
amdModules: resolver.options.amdModules,
amdModules,
fastbootOnlyAmdModules: resolver.options.fastbootOnlyAmdModules,
});
}
Expand Down
1 change: 1 addition & 0 deletions tests/scenarios/compat-resolver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ Scenarios.fromProject(() => new Project())
],
amdModules: [],
fastbootOnlyAmdModules: [],
testModules: [],
};

givenFiles({
Expand Down
32 changes: 28 additions & 4 deletions tests/scenarios/compat-route-split-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,20 @@ splitScenarios
.withContents(contents => {
const [, objectName] = /"my-app\/controllers\/index": (amdMod\d+),/.exec(contents) ?? [];

return contents.includes(`import * as ${objectName} from "my-app\/controllers/index.js";`);
return contents.includes(`import * as ${objectName} from "my-app/controllers/index.js";`);
}, 'controllers/index import/export should be present in the virtual -embroider-amd-modules.js file');
});

test('has non-split route templates in main entrypoint', function () {
expectFile('./assets/my-app.js').matches('templates/index');
expectAudit
.module('./assets/my-app.js')
.resolves('./-embroider-amd-modules.js')
.toModule()
.withContents(contents => {
const [, objectName] = /"my-app\/templates\/index": (amdMod\d+),/.exec(contents) ?? [];

return contents.includes(`import * as ${objectName} from "my-app/templates/index.hbs";`);
}, 'templates/index import/export should be present in the virtual -embroider-amd-modules.js file');
});

test('has non-split routes in main entrypoint', function () {
Expand Down Expand Up @@ -313,7 +321,15 @@ splitScenarios
});

test('has non-split route templates in main entrypoint', function () {
expectFile('./assets/my-app.js').matches('pods/index/template');
expectAudit
.module('./assets/my-app.js')
.resolves('./-embroider-amd-modules.js')
.toModule()
.withContents(contents => {
const [, objectName] = /"my-app\/pods\/index\/template": (amdMod\d+),/.exec(contents) ?? [];

return contents.includes(`import * as ${objectName} from "my-app\/pods\/index\/template.hbs";`);
}, 'pods/index/template import/export should be present in the virtual -embroider-amd-modules.js file');
});

test('has non-split routes in main entrypoint', function () {
Expand Down Expand Up @@ -501,7 +517,15 @@ splitScenarios
});

test('has non-split route templates in main entrypoint', function () {
expectFile('./assets/my-app.js').matches('routes/index/template');
expectAudit
.module('./assets/my-app.js')
.resolves('./-embroider-amd-modules.js')
.toModule()
.withContents(contents => {
const [, objectName] = /"my-app\/routes\/index\/template": (amdMod\d+),/.exec(contents) ?? [];

return contents.includes(`import * as ${objectName} from "my-app\/routes\/index\/template.hbs";`);
}, 'routes/index/template import/export should be present in the virtual -embroider-amd-modules.js file');
});

test('has non-split routes in main entrypoint', function () {
Expand Down
1 change: 1 addition & 0 deletions tests/scenarios/core-resolver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ Scenarios.fromProject(() => new Project())
],
amdModules: [],
fastbootOnlyAmdModules: [],
testModules: [],
};

givenFiles({
Expand Down

0 comments on commit 2a6a486

Please sign in to comment.