Skip to content

Commit

Permalink
feat(testlab): update sourceMappingURL when copying a JS file
Browse files Browse the repository at this point in the history
When a `.ts` compled `.js` file is copied and required, a warning / error is logged to console because the accompanying `.js.map` file cannot be resolved. This PR updates the sourceMappingURL in the copied file to point to the original `.js.map` file as an absolute path so it can be resolved (only if accompanying `.js.map` file exists).
  • Loading branch information
virkt25 committed Apr 11, 2018
1 parent 345a71f commit aac2781
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 50 deletions.
7 changes: 0 additions & 7 deletions packages/boot/test/acceptance/controller.booter.acceptance.ts
Expand Up @@ -32,17 +32,10 @@ describe('controller booter acceptance tests', () => {

async function getApp() {
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js'));
await sandbox.copyFile(
resolve(__dirname, '../fixtures/application.js.map'),
);
await sandbox.copyFile(
resolve(__dirname, '../fixtures/multiple.artifact.js'),
'controllers/multiple.controller.js',
);
await sandbox.copyFile(
resolve(__dirname, '../fixtures/multiple.artifact.js.map'),
'controllers/multiple.artifact.js.map',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
app = new MyApp();
Expand Down
Expand Up @@ -34,17 +34,10 @@ describe('controller booter integration tests', () => {

async function getApp() {
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js'));
await sandbox.copyFile(
resolve(__dirname, '../fixtures/application.js.map'),
);
await sandbox.copyFile(
resolve(__dirname, '../fixtures/multiple.artifact.js'),
'controllers/multiple.controller.js',
);
await sandbox.copyFile(
resolve(__dirname, '../fixtures/multiple.artifact.js.map'),
'controllers/multiple.artifact.js.map',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
app = new MyApp();
Expand Down
Expand Up @@ -34,17 +34,10 @@ describe('repository booter integration tests', () => {

async function getApp() {
await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js'));
await sandbox.copyFile(
resolve(__dirname, '../fixtures/application.js.map'),
);
await sandbox.copyFile(
resolve(__dirname, '../fixtures/multiple.artifact.js'),
'repositories/multiple.repository.js',
);
await sandbox.copyFile(
resolve(__dirname, '../fixtures/multiple.artifact.js.map'),
'repositories/multiple.artifact.js.map',
);

const MyApp = require(resolve(SANDBOX_PATH, 'application.js')).BooterApp;
app = new MyApp();
Expand Down
13 changes: 0 additions & 13 deletions packages/boot/test/unit/booters/booter-utils.unit.ts
Expand Up @@ -45,17 +45,10 @@ describe('booter-utils unit tests', () => {
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/empty.artifact.js'),
);
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/empty.artifact.js.map'),
);
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/multiple.artifact.js'),
'nested/multiple.artifact.js',
);
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/multiple.artifact.js.map'),
'nested/multiple.artifact.js.map',
);
}
});

Expand All @@ -71,9 +64,6 @@ describe('booter-utils unit tests', () => {
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/multiple.artifact.js'),
);
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/multiple.artifact.js.map'),
);
const files = [resolve(SANDBOX_PATH, 'multiple.artifact.js')];
const NUM_CLASSES = 2; // Number of classes in above file

Expand All @@ -87,9 +77,6 @@ describe('booter-utils unit tests', () => {
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/empty.artifact.js'),
);
await sandbox.copyFile(
resolve(__dirname, '../../fixtures/empty.artifact.js.map'),
);
const files = [resolve(SANDBOX_PATH, 'empty.artifact.js')];

const classes = loadClassesFromFiles(files);
Expand Down
23 changes: 21 additions & 2 deletions packages/testlab/src/test-sandbox.ts
Expand Up @@ -4,7 +4,15 @@
// License text available at https://opensource.org/licenses/MIT

import {resolve, parse} from 'path';
import {copy, ensureDirSync, emptyDir, remove, ensureDir} from 'fs-extra';
import {
copy,
ensureDirSync,
emptyDir,
remove,
ensureDir,
pathExists,
appendFile,
} from 'fs-extra';

/**
* TestSandbox class provides a convenient way to get a reference to a
Expand Down Expand Up @@ -64,6 +72,7 @@ export class TestSandbox {

/**
* Makes a directory in the TestSandbox
*
* @param dir Name of directory to create (relative to TestSandbox path)
*/
async mkdir(dir: string): Promise<void> {
Expand All @@ -72,7 +81,11 @@ export class TestSandbox {
}

/**
* Copies a file from src to the TestSandbox.
* Copies a file from src to the TestSandbox. If copying a `.js` file which
* has an accompanying `.js.map` file in the src file location, the dest file
* will have its sourceMappingURL updated to point to the original file as
* an absolute path so you don't need to copy the map file.
*
* @param src Absolute path of file to be copied to the TestSandbox
* @param [dest] Optional. Destination filename of the copy operation
* (relative to TestSandbox). Original filename used if not specified.
Expand All @@ -82,6 +95,12 @@ export class TestSandbox {
dest = dest
? resolve(this.path, dest)
: resolve(this.path, parse(src).base);

await copy(src, dest);

if (parse(src).ext === '.js' && pathExists(src + '.map')) {
const srcMap = src + '.map';
await appendFile(dest, `\n//# sourceMappingURL=${srcMap}`);
}
}
}
3 changes: 3 additions & 0 deletions packages/testlab/test/fixtures/test.ts
@@ -0,0 +1,3 @@
export function main(): string {
return 'Hello world';
}
35 changes: 21 additions & 14 deletions packages/testlab/test/integration/test-sandbox.integration.ts
Expand Up @@ -16,9 +16,7 @@ describe('TestSandbox integration tests', () => {
'../../../test/fixtures',
COPY_FILE,
);
let fileContent: string;

before(getCopyFileContents);
beforeEach(createSandbox);
beforeEach(givenPath);
afterEach(deleteSandbox);
Expand All @@ -37,24 +35,36 @@ describe('TestSandbox integration tests', () => {
it('copies a file to the sandbox', async () => {
await sandbox.copyFile(COPY_FILE_PATH);
expect(await pathExists(resolve(path, COPY_FILE))).to.be.True();
await compareFiles(resolve(path, COPY_FILE));
await expectFilesToBeIdentical(COPY_FILE_PATH, resolve(path, COPY_FILE));
});

it('copies and renames the file to the sandbox', async () => {
const rename = 'copy.me.js';
await sandbox.copyFile(COPY_FILE_PATH, rename);
expect(await pathExists(resolve(path, COPY_FILE))).to.be.False();
expect(await pathExists(resolve(path, rename))).to.be.True();
await compareFiles(resolve(path, rename));
await expectFilesToBeIdentical(COPY_FILE_PATH, resolve(path, rename));
});

it('copies file to a directory', async () => {
const dir = 'test';
await sandbox.mkdir(dir);
const rename = `${dir}/${COPY_FILE}`;
await sandbox.copyFile(COPY_FILE_PATH, rename);
expect(await pathExists(resolve(path, rename))).to.be.True();
await compareFiles(resolve(path, rename));
await expectFilesToBeIdentical(COPY_FILE_PATH, resolve(path, rename));
});

it('updates source map path for a copied file', async () => {
const file = 'test.js';
const resolvedFile = resolve(__dirname, '../fixtures/test.js');
const sourceMapString = `//# sourceMappingURL=${resolvedFile}.map`;

await sandbox.copyFile(resolvedFile);
let fileContents = (await readFile(resolve(path, file), 'utf8')).split(
'\n',
);

expect(fileContents.pop()).to.equal(sourceMapString);
});

it('deletes the test sandbox', async () => {
Expand Down Expand Up @@ -93,13 +103,14 @@ describe('TestSandbox integration tests', () => {
await sandbox.delete();
}

async function compareFiles(path1: string) {
const file = await readFile(path1, 'utf8');
expect(file).to.equal(fileContent);
async function expectFilesToBeIdentical(original: string, copied: string) {
const originalContent = await readFile(original, 'utf8');
const copiedContent = await readFile(copied, 'utf8');
expect(copiedContent).to.equal(originalContent);
}

function createSandbox() {
sandbox = new TestSandbox(resolve(__dirname, 'sandbox'));
sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox'));
}

function givenPath() {
Expand All @@ -110,8 +121,4 @@ describe('TestSandbox integration tests', () => {
if (!await pathExists(path)) return;
await remove(sandbox.getPath());
}

async function getCopyFileContents() {
fileContent = await readFile(COPY_FILE_PATH, 'utf8');
}
});

0 comments on commit aac2781

Please sign in to comment.