From 6281fd230a88bbbe848b945885a198ce6131b1e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 15 Jun 2020 12:21:09 +0200 Subject: [PATCH] Revert "fix(boot): make sure sandbox directories are removed after tests" This reverts commit cbba7249dade6e5b6b2e9366d7e99a513cd7c7cd. --- .../application-metadata.booter.acceptance.ts | 21 +++++++++---------- ...component-application.booter.acceptance.ts | 16 ++++++-------- .../controller.booter.acceptance.ts | 8 ++++--- .../crud-rest.api-builder.acceptance.ts | 5 ++--- .../acceptance/model-api.booter.acceptance.ts | 8 ++++--- .../src/__tests__/fixtures/application.ts | 4 ++++ .../controller.booter.integration.ts | 2 -- .../datasource.booter.integration.ts | 2 -- .../interceptor.booter.integration.ts | 2 -- .../lifecycle-observer.booter.integration.ts | 2 -- 10 files changed, 32 insertions(+), 38 deletions(-) diff --git a/packages/boot/src/__tests__/acceptance/application-metadata.booter.acceptance.ts b/packages/boot/src/__tests__/acceptance/application-metadata.booter.acceptance.ts index b22de6d5e316..6da946e3dba4 100644 --- a/packages/boot/src/__tests__/acceptance/application-metadata.booter.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/application-metadata.booter.acceptance.ts @@ -5,16 +5,19 @@ import {CoreBindings} from '@loopback/core'; import {expect, givenHttpServerConfig, TestSandbox} from '@loopback/testlab'; -import fs from 'fs'; import {resolve} from 'path'; import {BooterApp} from '../fixtures/application'; describe('application metadata booter acceptance tests', () => { let app: BooterApp; - const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox')); + const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'), { + // We intentionally use this flag so that `dist/application.js` can keep + // its relative path to satisfy import statements + subdir: false, + }); + beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); - after('delete sandbox', () => sandbox.delete()); it('binds content of package.json to application metadata', async () => { await app.boot(); @@ -30,18 +33,14 @@ describe('application metadata booter acceptance tests', () => { // Add the following files // - package.json // - dist/application.js - - const appJsFile = resolve(__dirname, '../fixtures/application.js'); - let appJs = fs.readFileSync(appJsFile, 'utf-8'); - // Adjust the relative path for `import` - appJs = appJs.replace('../..', '../../..'); - await sandbox.writeTextFile('dist/application.js', appJs); - await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json')); + await sandbox.copyFile( + resolve(__dirname, '../fixtures/application.js'), + 'dist/application.js', + ); const MyApp = require(resolve(sandbox.path, 'dist/application.js')) .BooterApp; - app = new MyApp({ rest: givenHttpServerConfig(), }); diff --git a/packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts b/packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts index ce4d3d4d90e1..2f87ddb53c07 100644 --- a/packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/component-application.booter.acceptance.ts @@ -5,7 +5,6 @@ import {Application, Component} from '@loopback/core'; import {expect, givenHttpServerConfig, TestSandbox} from '@loopback/testlab'; -import fs from 'fs'; import {resolve} from 'path'; import {BootMixin, createComponentApplicationBooterBinding} from '../..'; import {bindingKeysExcludedFromSubApp} from '../../booters'; @@ -14,13 +13,15 @@ import {BooterApp} from '../fixtures/application'; describe('component application booter acceptance tests', () => { let app: BooterApp; - const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox')); + const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'), { + // We intentionally use this flag so that `dist/application.js` can keep + // its relative path to satisfy import statements + subdir: false, + }); beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); - after('delete sandbox', () => sandbox.delete()); - it('binds artifacts booted from the component application', async () => { class BooterAppComponent implements Component { bindings = [createComponentApplicationBooterBinding(app)]; @@ -86,13 +87,8 @@ describe('component application booter acceptance tests', () => { } async function getApp() { - const appJsFile = resolve(__dirname, '../fixtures/application.js'); - let appJs = fs.readFileSync(appJsFile, 'utf-8'); - // Adjust the relative path for `import` - appJs = appJs.replace('../..', '../../..'); - await sandbox.writeTextFile('application.js', appJs); - await sandbox.copyFile(resolve(__dirname, '../fixtures/package.json')); + await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); await sandbox.copyFile( resolve(__dirname, '../fixtures/multiple.artifact.js'), 'controllers/multiple.controller.js', diff --git a/packages/boot/src/__tests__/acceptance/controller.booter.acceptance.ts b/packages/boot/src/__tests__/acceptance/controller.booter.acceptance.ts index 9ddd45048622..5104728c21b7 100644 --- a/packages/boot/src/__tests__/acceptance/controller.booter.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/controller.booter.acceptance.ts @@ -20,8 +20,6 @@ describe('controller booter acceptance tests', () => { afterEach(stopApp); - after('delete sandbox', () => sandbox.delete()); - it('binds controllers using ControllerDefaults and REST endpoints work', async () => { await app.boot(); await app.start(); @@ -48,6 +46,10 @@ describe('controller booter acceptance tests', () => { } async function stopApp() { - await app?.stop(); + try { + await app.stop(); + } catch (err) { + console.log(`Stopping the app threw an error: ${err}`); + } } }); diff --git a/packages/boot/src/__tests__/acceptance/crud-rest.api-builder.acceptance.ts b/packages/boot/src/__tests__/acceptance/crud-rest.api-builder.acceptance.ts index d045a5b3562b..241d0c6ddb35 100644 --- a/packages/boot/src/__tests__/acceptance/crud-rest.api-builder.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/crud-rest.api-builder.acceptance.ts @@ -21,8 +21,6 @@ describe('CRUD rest builder acceptance tests', () => { afterEach(stopApp); - after('delete sandbox', () => sandbox.delete()); - it('binds the controller and repository to the application', async () => { await sandbox.copyFile( resolve(__dirname, '../fixtures/product.model.js'), @@ -167,6 +165,7 @@ module.exports = { } async function stopApp() { - if (app?.state === 'started') await app?.stop(); + if (app.state !== 'started') return; + await app.stop(); } }); diff --git a/packages/boot/src/__tests__/acceptance/model-api.booter.acceptance.ts b/packages/boot/src/__tests__/acceptance/model-api.booter.acceptance.ts index 4451f9bf1c7e..f1ccfe6babce 100644 --- a/packages/boot/src/__tests__/acceptance/model-api.booter.acceptance.ts +++ b/packages/boot/src/__tests__/acceptance/model-api.booter.acceptance.ts @@ -33,8 +33,6 @@ describe('model API booter acceptance tests', () => { afterEach(stopApp); - after('delete sandbox', () => sandbox.delete()); - it('uses the correct model API builder', async () => { await sandbox.copyFile( resolve(__dirname, '../fixtures/product.model.js'), @@ -169,6 +167,10 @@ module.exports = { } async function stopApp() { - if (app?.state === 'started') await app?.stop(); + try { + await app.stop(); + } catch (err) { + // console.error('Cannot stop the app, ignoring the error.', err); + } } }); diff --git a/packages/boot/src/__tests__/fixtures/application.ts b/packages/boot/src/__tests__/fixtures/application.ts index ced9bb0436ca..7ad7acc0e148 100644 --- a/packages/boot/src/__tests__/fixtures/application.ts +++ b/packages/boot/src/__tests__/fixtures/application.ts @@ -9,6 +9,10 @@ import {RestApplication} from '@loopback/rest'; import {ServiceMixin} from '@loopback/service-proxy'; import {BootMixin} from '../..'; +// Force package.json to be copied to `dist` by `tsc` +// eslint-disable-next-line @typescript-eslint/no-unused-vars +import * as pkg from './package.json'; + export class BooterApp extends BootMixin( ServiceMixin(RepositoryMixin(RestApplication)), ) { diff --git a/packages/boot/src/__tests__/integration/controller.booter.integration.ts b/packages/boot/src/__tests__/integration/controller.booter.integration.ts index c8450f893d7a..8f10b3fb47c9 100644 --- a/packages/boot/src/__tests__/integration/controller.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/controller.booter.integration.ts @@ -19,8 +19,6 @@ describe('controller booter integration tests', () => { beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); - after('delete sandbox', () => sandbox.delete()); - it('boots controllers when app.boot() is called', async () => { const expectedBindings = [ `${CONTROLLERS_PREFIX}.ArtifactOne`, diff --git a/packages/boot/src/__tests__/integration/datasource.booter.integration.ts b/packages/boot/src/__tests__/integration/datasource.booter.integration.ts index 0316cc265396..c7eb0d86d2cb 100644 --- a/packages/boot/src/__tests__/integration/datasource.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/datasource.booter.integration.ts @@ -18,8 +18,6 @@ describe('datasource booter integration tests', () => { beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); - after('delete sandbox', () => sandbox.delete()); - it('boots datasources when app.boot() is called', async () => { const expectedBindings = [`${DATASOURCES_PREFIX}.db`]; diff --git a/packages/boot/src/__tests__/integration/interceptor.booter.integration.ts b/packages/boot/src/__tests__/integration/interceptor.booter.integration.ts index 2de23fd83724..58a608eba188 100644 --- a/packages/boot/src/__tests__/integration/interceptor.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/interceptor.booter.integration.ts @@ -20,8 +20,6 @@ describe('interceptor script booter integration tests', () => { beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(buildAppWithInterceptors); - after('delete sandbox', () => sandbox.delete()); - it('boots global interceptors when app.boot() is called', async () => { const expectedBinding = { key: `${GLOBAL_INTERCEPTOR_NAMESPACE}.myGlobalInterceptor`, diff --git a/packages/boot/src/__tests__/integration/lifecycle-observer.booter.integration.ts b/packages/boot/src/__tests__/integration/lifecycle-observer.booter.integration.ts index 12c8bb0236b1..933b3755c54b 100644 --- a/packages/boot/src/__tests__/integration/lifecycle-observer.booter.integration.ts +++ b/packages/boot/src/__tests__/integration/lifecycle-observer.booter.integration.ts @@ -24,8 +24,6 @@ describe('lifecycle script booter integration tests', () => { beforeEach('reset sandbox', () => sandbox.reset()); beforeEach(getApp); - after('delete sandbox', () => sandbox.delete()); - it('boots life cycle observers when app.boot() is called', async () => { const expectedBinding = { key: `${OBSERVER_PREFIX}.MyLifeCycleObserver`,