Skip to content

Commit

Permalink
feat(boot): support loading ESM artifacts
Browse files Browse the repository at this point in the history
see: #10744
Signed-off-by: Rifa Achrinza <[email protected]>
  • Loading branch information
achrinza committed Nov 17, 2024
1 parent b5b2712 commit 48cd7db
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
29 changes: 25 additions & 4 deletions packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import {expect, TestSandbox} from '@loopback/testlab';
import {resolve} from 'path';
import {discoverFiles, isClass, loadClassesFromFiles} from '../../..';
import { readFileSync } from 'fs';

Check failure on line 9 in packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

View workflow job for this annotation

GitHub Actions / Code Lint

Replace `·readFileSync·` with `readFileSync`

Check failure on line 9 in packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

View workflow job for this annotation

GitHub Actions / Code Lint

'readFileSync' is defined but never used

describe('booter-utils unit tests', () => {
const sandbox = new TestSandbox(resolve(__dirname, '../../../.sandbox'));
Expand Down Expand Up @@ -57,7 +58,7 @@ describe('booter-utils unit tests', () => {
});
});

describe('loadClassesFromFiles()', () => {
describe('loadClassesFromFiles()', async () => {

Check failure on line 61 in packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

View workflow job for this annotation

GitHub Actions / Code Lint

Promise returned in function argument where a void return was expected
it('returns an array of classes from a file', async () => {
// Copying a test file to sandbox that contains a function and 2 classes
await sandbox.copyFile(
Expand All @@ -66,26 +67,46 @@ describe('booter-utils unit tests', () => {
const files = [resolve(sandbox.path, 'multiple.artifact.js')];
const NUM_CLASSES = 2; // Number of classes in above file

const classes = loadClassesFromFiles(files, sandbox.path);
const classes = await loadClassesFromFiles(files, sandbox.path);
expect(classes).to.have.lengthOf(NUM_CLASSES);
expect(classes[0]).to.be.a.Function();
expect(classes[1]).to.be.a.Function();
});

Check failure on line 75 in packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

View workflow job for this annotation

GitHub Actions / Code Lint

Delete `····`
it('loads classes from CJS files', async () => {
const artifactFilename = 'multiple.artifact.cjs';

Check failure on line 77 in packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

View workflow job for this annotation

GitHub Actions / Code Lint

Delete `··`
await sandbox.copyFile(resolve(__dirname, '../../fixtures/multiple.artifact.cjs'));

Check failure on line 78 in packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

View workflow job for this annotation

GitHub Actions / Code Lint

Replace `········await·sandbox.copyFile(resolve(__dirname,·'../../fixtures/multiple.artifact.cjs'));` with `······await·sandbox.copyFile(⏎········resolve(__dirname,·'../../fixtures/multiple.artifact.cjs'),`
const NUM_CLASSES = 2;

Check failure on line 79 in packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

View workflow job for this annotation

GitHub Actions / Code Lint

Insert `····);⏎`
const classes = await loadClassesFromFiles([resolve(sandbox.path, artifactFilename)], sandbox.path);

Check failure on line 80 in packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

View workflow job for this annotation

GitHub Actions / Code Lint

Replace `········const·classes·=·await·loadClassesFromFiles([resolve(sandbox.path,·artifactFilename)],·sandbox.path);` with `······const·classes·=·await·loadClassesFromFiles(⏎········[resolve(sandbox.path,·artifactFilename)],⏎········sandbox.path,`
expect(classes).to.have.lengthOf(NUM_CLASSES);

Check failure on line 81 in packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts

View workflow job for this annotation

GitHub Actions / Code Lint

Replace `··` with `······);⏎`
expect(classes[0]).to.be.a.Function();
expect(classes[1]).to.be.a.Function();
});

it('loads classes from ESM files', async () => {
const artifactFilename = 'multiple.artifact.mjs';
await sandbox.copyFile(resolve(__dirname, '../../fixtures/multiple.artifact.mjs'));
const NUM_CLASSES = 2;
const classes = await loadClassesFromFiles([resolve(sandbox.path, artifactFilename)], sandbox.path);
expect(classes).to.have.lengthOf(NUM_CLASSES);
expect(classes[0]).to.be.a.Function();
expect(classes[1]).to.be.a.Function();
});

it('returns an empty array given an empty file', async () => {
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/empty.artifact.js'),
);
const files = [resolve(sandbox.path, 'empty.artifact.js')];

const classes = loadClassesFromFiles(files, sandbox.path);
const classes = await loadClassesFromFiles(files, sandbox.path);
expect(classes).to.be.an.Array();
expect(classes).to.be.empty();
});

it('throws an error given a non-existent file', async () => {
const files = [resolve(sandbox.path, 'fake.artifact.js')];
expect(() => loadClassesFromFiles(files, sandbox.path)).to.throw(
expect(loadClassesFromFiles(files, sandbox.path)).to.be.rejectedWith(
/Cannot find module/,
);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/boot/src/booters/base-artifact.booter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,6 @@ export class BaseArtifactBooter implements Booter {
* and then process the artifact classes as appropriate.
*/
async load() {
this.classes = loadClassesFromFiles(this.discovered, this.projectRoot);
this.classes = await loadClassesFromFiles(this.discovered, this.projectRoot);
}
}

0 comments on commit 48cd7db

Please sign in to comment.