diff --git a/packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts b/packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts index 769ebe638e81..b47c0e1c0ac6 100644 --- a/packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts +++ b/packages/boot/src/__tests__/unit/booters/booter-utils.unit.ts @@ -6,6 +6,7 @@ import {expect, TestSandbox} from '@loopback/testlab'; import {resolve} from 'path'; import {discoverFiles, isClass, loadClassesFromFiles} from '../../..'; +import { readFileSync } from 'fs'; describe('booter-utils unit tests', () => { const sandbox = new TestSandbox(resolve(__dirname, '../../../.sandbox')); @@ -57,7 +58,7 @@ describe('booter-utils unit tests', () => { }); }); - describe('loadClassesFromFiles()', () => { + describe('loadClassesFromFiles()', async () => { 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( @@ -66,11 +67,31 @@ 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(); }); + + it('loads classes from CJS files', async () => { + const artifactFilename = 'multiple.artifact.cjs'; + await sandbox.copyFile(resolve(__dirname, '../../fixtures/multiple.artifact.cjs')); + 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('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( @@ -78,14 +99,14 @@ describe('booter-utils unit tests', () => { ); 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/, ); }); diff --git a/packages/boot/src/booters/base-artifact.booter.ts b/packages/boot/src/booters/base-artifact.booter.ts index 50150a50bbd0..ae2aad4f655c 100644 --- a/packages/boot/src/booters/base-artifact.booter.ts +++ b/packages/boot/src/booters/base-artifact.booter.ts @@ -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); } }