Skip to content

Commit cbba724

Browse files
committed
fix(boot): make sure sandbox directories are removed after tests
1 parent 5b4f324 commit cbba724

10 files changed

+38
-32
lines changed

packages/boot/src/__tests__/acceptance/application-metadata.booter.acceptance.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55

66
import {CoreBindings} from '@loopback/core';
77
import {expect, givenHttpServerConfig, TestSandbox} from '@loopback/testlab';
8+
import fs from 'fs';
89
import {resolve} from 'path';
910
import {BooterApp} from '../fixtures/application';
1011

1112
describe('application metadata booter acceptance tests', () => {
1213
let app: BooterApp;
13-
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'), {
14-
// We intentionally use this flag so that `dist/application.js` can keep
15-
// its relative path to satisfy import statements
16-
subdir: false,
17-
});
14+
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));
1815

19-
beforeEach('reset sandbox', () => sandbox.reset());
2016
beforeEach(getApp);
17+
after('delete sandbox', () => sandbox.delete());
2118

2219
it('binds content of package.json to application metadata', async () => {
2320
await app.boot();
@@ -33,14 +30,18 @@ describe('application metadata booter acceptance tests', () => {
3330
// Add the following files
3431
// - package.json
3532
// - dist/application.js
33+
34+
const appJsFile = resolve(__dirname, '../fixtures/application.js');
35+
let appJs = fs.readFileSync(appJsFile, 'utf-8');
36+
// Adjust the relative path for `import`
37+
appJs = appJs.replace('../..', '../../..');
38+
await sandbox.writeTextFile('dist/application.js', appJs);
39+
3640
await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json'));
37-
await sandbox.copyFile(
38-
resolve(__dirname, '../fixtures/application.js'),
39-
'dist/application.js',
40-
);
4141

4242
const MyApp = require(resolve(sandbox.path, 'dist/application.js'))
4343
.BooterApp;
44+
4445
app = new MyApp({
4546
rest: givenHttpServerConfig(),
4647
});

packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import {Application, Component} from '@loopback/core';
77
import {expect, givenHttpServerConfig, TestSandbox} from '@loopback/testlab';
8+
import fs from 'fs';
89
import {resolve} from 'path';
910
import {BootMixin, createComponentApplicationBooterBinding} from '../..';
1011
import {bindingKeysExcludedFromSubApp} from '../../booters';
@@ -13,15 +14,13 @@ import {BooterApp} from '../fixtures/application';
1314

1415
describe('component application booter acceptance tests', () => {
1516
let app: BooterApp;
16-
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'), {
17-
// We intentionally use this flag so that `dist/application.js` can keep
18-
// its relative path to satisfy import statements
19-
subdir: false,
20-
});
17+
const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));
2118

2219
beforeEach('reset sandbox', () => sandbox.reset());
2320
beforeEach(getApp);
2421

22+
after('delete sandbox', () => sandbox.delete());
23+
2524
it('binds artifacts booted from the component application', async () => {
2625
class BooterAppComponent implements Component {
2726
bindings = [createComponentApplicationBooterBinding(app)];
@@ -87,8 +86,13 @@ describe('component application booter acceptance tests', () => {
8786
}
8887

8988
async function getApp() {
89+
const appJsFile = resolve(__dirname, '../fixtures/application.js');
90+
let appJs = fs.readFileSync(appJsFile, 'utf-8');
91+
// Adjust the relative path for `import`
92+
appJs = appJs.replace('../..', '../../..');
93+
await sandbox.writeTextFile('application.js', appJs);
94+
9095
await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json'));
91-
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js'));
9296
await sandbox.copyFile(
9397
resolve(__dirname, '../fixtures/multiple.artifact.js'),
9498
'controllers/multiple.controller.js',

packages/boot/src/__tests__/acceptance/controller.booter.acceptance.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ describe('controller booter acceptance tests', () => {
2020

2121
afterEach(stopApp);
2222

23+
after('delete sandbox', () => sandbox.delete());
24+
2325
it('binds controllers using ControllerDefaults and REST endpoints work', async () => {
2426
await app.boot();
2527
await app.start();
@@ -46,10 +48,6 @@ describe('controller booter acceptance tests', () => {
4648
}
4749

4850
async function stopApp() {
49-
try {
50-
await app.stop();
51-
} catch (err) {
52-
console.log(`Stopping the app threw an error: ${err}`);
53-
}
51+
await app?.stop();
5452
}
5553
});

packages/boot/src/__tests__/acceptance/crud-rest.api-builder.acceptance.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ describe('CRUD rest builder acceptance tests', () => {
2121

2222
afterEach(stopApp);
2323

24+
after('delete sandbox', () => sandbox.delete());
25+
2426
it('binds the controller and repository to the application', async () => {
2527
await sandbox.copyFile(
2628
resolve(__dirname, '../fixtures/product.model.js'),
@@ -165,7 +167,6 @@ module.exports = {
165167
}
166168

167169
async function stopApp() {
168-
if (app.state !== 'started') return;
169-
await app.stop();
170+
if (app?.state === 'started') await app?.stop();
170171
}
171172
});

packages/boot/src/__tests__/acceptance/model-api.booter.acceptance.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ describe('model API booter acceptance tests', () => {
3333

3434
afterEach(stopApp);
3535

36+
after('delete sandbox', () => sandbox.delete());
37+
3638
it('uses the correct model API builder', async () => {
3739
await sandbox.copyFile(
3840
resolve(__dirname, '../fixtures/product.model.js'),
@@ -167,10 +169,6 @@ module.exports = {
167169
}
168170

169171
async function stopApp() {
170-
try {
171-
await app.stop();
172-
} catch (err) {
173-
// console.error('Cannot stop the app, ignoring the error.', err);
174-
}
172+
if (app?.state === 'started') await app?.stop();
175173
}
176174
});

packages/boot/src/__tests__/fixtures/application.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import {RestApplication} from '@loopback/rest';
99
import {ServiceMixin} from '@loopback/service-proxy';
1010
import {BootMixin} from '../..';
1111

12-
// Force package.json to be copied to `dist` by `tsc`
13-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
14-
import * as pkg from './package.json';
15-
1612
export class BooterApp extends BootMixin(
1713
ServiceMixin(RepositoryMixin(RestApplication)),
1814
) {

packages/boot/src/__tests__/integration/controller.booter.integration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ describe('controller booter integration tests', () => {
1919
beforeEach('reset sandbox', () => sandbox.reset());
2020
beforeEach(getApp);
2121

22+
after('delete sandbox', () => sandbox.delete());
23+
2224
it('boots controllers when app.boot() is called', async () => {
2325
const expectedBindings = [
2426
`${CONTROLLERS_PREFIX}.ArtifactOne`,

packages/boot/src/__tests__/integration/datasource.booter.integration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ describe('datasource booter integration tests', () => {
1818
beforeEach('reset sandbox', () => sandbox.reset());
1919
beforeEach(getApp);
2020

21+
after('delete sandbox', () => sandbox.delete());
22+
2123
it('boots datasources when app.boot() is called', async () => {
2224
const expectedBindings = [`${DATASOURCES_PREFIX}.db`];
2325

packages/boot/src/__tests__/integration/interceptor.booter.integration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ describe('interceptor script booter integration tests', () => {
2020
beforeEach('reset sandbox', () => sandbox.reset());
2121
beforeEach(buildAppWithInterceptors);
2222

23+
after('delete sandbox', () => sandbox.delete());
24+
2325
it('boots global interceptors when app.boot() is called', async () => {
2426
const expectedBinding = {
2527
key: `${GLOBAL_INTERCEPTOR_NAMESPACE}.myGlobalInterceptor`,

packages/boot/src/__tests__/integration/lifecycle-observer.booter.integration.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ describe('lifecycle script booter integration tests', () => {
2424
beforeEach('reset sandbox', () => sandbox.reset());
2525
beforeEach(getApp);
2626

27+
after('delete sandbox', () => sandbox.delete());
28+
2729
it('boots life cycle observers when app.boot() is called', async () => {
2830
const expectedBinding = {
2931
key: `${OBSERVER_PREFIX}.MyLifeCycleObserver`,

0 commit comments

Comments
 (0)