Skip to content

Commit

Permalink
fix: esbuild messages printed out twice (#212)
Browse files Browse the repository at this point in the history
* chore: update jest launch config

* fix: esbuild messages printed out twice
  • Loading branch information
mrgrain authored Aug 13, 2022
1 parent d50ddb9 commit 2596368
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 119 deletions.
35 changes: 26 additions & 9 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
vscode,
} from 'projen';
import { ReleaseTrigger } from 'projen/lib/release';
import { InternalConsoleOptions } from 'projen/lib/vscode';
import { SourceFile } from 'ts-morph';
import { tagOnNpm } from './projenrc/release';
import { TypeScriptSourceFile } from './projenrc/TypeScriptSourceFile';
Expand Down Expand Up @@ -113,7 +114,7 @@ const project = new awscdk.AwsCdkConstructLibrary({
'API.md',
'CHANGELOG.md',
'CONTRIBUTING.md',
'SECURITY.md'
'SECURITY.md',
],
});

Expand Down Expand Up @@ -205,14 +206,30 @@ new JsonFile(project, '.vscode/settings.json', {
},
});

new vscode.VsCode(project).launchConfiguration.addConfiguration({
type: 'node',
name: 'vscode-jest-tests',
request: 'launch',
internalConsoleOptions: vscode.InternalConsoleOptions.NEVER_OPEN,
program: '${workspaceFolder}/node_modules/.bin/jest',
args: ['--runInBand', '--watchAll=false'],
});
new vscode.VsCode(project).launchConfiguration.addConfiguration(
{
type: 'node',
name: 'vscode-jest-tests.v2',
request: 'launch',
internalConsoleOptions: InternalConsoleOptions.NEVER_OPEN,
program: '${workspaceFolder}/node_modules/.bin/jest',
args: [
'--runInBand',
'--watchAll=false',
'--testNamePattern',
'${jest.testNamePattern}',
'--runTestsByPath',
'${jest.testFile}',
],
// Not supported by projen:
// console: 'integratedTerminal',
// disableOptimisticBPs: true,
// cwd: '${workspaceFolder}',
});
const launchConfig = project.tryFindObjectFile('.vscode/launch.json');
launchConfig?.addOverride('configurations.0.console', 'integratedTerminal');
launchConfig?.addOverride('configurations.0.disableOptimisticBPs', true);
launchConfig?.addOverride('configurations.0.cwd', '${workspaceFolder}');


// esbuild
Expand Down
13 changes: 10 additions & 3 deletions .vscode/launch.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions src/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import {
FileSystem,
ILocalBundling,
} from 'aws-cdk-lib';
import { BuildFailure, BuildOptions, BuildResult } from './esbuild-types';
import { BuildOptions } from './esbuild-types';
import { buildSync, wrapWithEsbuildBinaryPath } from './esbuild-wrapper';
import { printBuildMessages } from './formatMessages';

/**
* A path or list or map of paths to the entry points of your code.
Expand Down Expand Up @@ -177,15 +176,12 @@ export class EsbuildBundler {

try {
const { buildFn = buildSync } = this.props;
const buildResult: BuildResult = wrapWithEsbuildBinaryPath(buildFn, this.props.esbuildBinaryPath)({
wrapWithEsbuildBinaryPath(buildFn, this.props.esbuildBinaryPath)({
entryPoints,
...(this.props?.buildOptions || {}),
...this.getOutputOptions(outputDir, { normalize, join }),
});

printBuildMessages(buildResult, { prefix: 'Build ' });
} catch (error) {
printBuildMessages(error as BuildFailure, { prefix: 'Build ' });
} catch (_unused) {
}

return true;
Expand Down
1 change: 0 additions & 1 deletion src/esbuild-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ function esbuild() {
}

export const buildSync = esbuild().buildSync;
export const formatMessagesSync = esbuild().formatMessagesSync;
export const transformSync = esbuild().transformSync;

export function wrapWithEsbuildBinaryPath<T extends CallableFunction>(fn: T, esbuildBinaryPath?: string) {
Expand Down
70 changes: 0 additions & 70 deletions src/formatMessages.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/inline-code.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { InlineCode } from 'aws-cdk-lib/aws-lambda';
import { TransformOptions, Loader, TransformFailure } from './esbuild-types';
import { TransformOptions, Loader } from './esbuild-types';
import { transformSync, wrapWithEsbuildBinaryPath } from './esbuild-wrapper';
import { printBuildMessages } from './formatMessages';

/**
* @stability experimental
Expand Down Expand Up @@ -51,15 +50,15 @@ abstract class BaseInlineCode extends InlineCode {
} = props;

try {
console.log = () => {};
console.error = () => {};
const transformedCode = wrapWithEsbuildBinaryPath(transformFn, esbuildBinaryPath)(code, {
logLevel: 'warning',
...transformOptions,
});
printBuildMessages(transformedCode, { prefix: 'Transform ' });

super(transformedCode.code);
} catch (error) {
printBuildMessages(error as TransformFailure, { prefix: 'Transform ' });

throw new Error('Failed to transform InlineCode');
}
}
Expand Down
9 changes: 0 additions & 9 deletions test/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ import { mocked } from 'jest-mock';
import { EsbuildBundler } from '../src/bundler';
import { BuildOptions, BuildResult } from '../src/esbuild-types';
import { buildSync } from '../src/esbuild-wrapper';
import { printBuildMessages } from '../src/formatMessages';

jest.mock('../src/formatMessages', () => ({
printBuildMessages: jest.fn(),
}));

jest.mock('esbuild', () => ({
buildSync: jest.fn(),
Expand All @@ -16,10 +11,6 @@ jest.mock('esbuild', () => ({
const realEsbuild = jest.requireActual('esbuild');

describe('bundling', () => {
beforeEach(() => {
mocked(printBuildMessages).mockReset();
});

describe('Given a project root path', () => {
it('should keep the relative path for the local bundler', () => {
const bundler = new EsbuildBundler(
Expand Down
66 changes: 51 additions & 15 deletions test/inline-code.test.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { Stack } from 'aws-cdk-lib';
import { mocked } from 'jest-mock';
import { transformSync } from '../lib/esbuild-wrapper';
import {
InlineJavaScriptCode,
InlineJsxCode,
InlineTsxCode,
InlineTypeScriptCode,
} from '../src';
import { printBuildMessages } from '../src/formatMessages';

jest.mock('../src/formatMessages', () => ({
printBuildMessages: jest.fn(),
}));

describe('using transformOptions', () => {
beforeEach(() => {
mocked(printBuildMessages).mockReset();
});

describe('given a banner code', () => {
it('should add the banner before the code', () => {
const code = new InlineJavaScriptCode(
Expand All @@ -34,10 +26,6 @@ describe('using transformOptions', () => {
});

describe('using transformerProps', () => {
beforeEach(() => {
mocked(printBuildMessages).mockReset();
});

describe('given some js code', () => {
it('should transform the code', () => {
const code = new InlineJavaScriptCode(
Expand Down Expand Up @@ -89,13 +77,27 @@ describe('using transformerProps', () => {
});

describe('given some broken ts code', () => {
it('should display errors and warnings', () => {
it('should throws', () => {
expect(() => {
const code = new InlineTypeScriptCode('let : d ===== 1');
code.bind(new Stack());
}).toThrowError('Failed to transform InlineCode');
});

// Currently no way to capture esbuild output,
// See https://github.com/evanw/esbuild/issues/2466
it.skip('should display an error', () => {
const originalConsole = console.error;
console.error = jest.fn();

expect(() => {
const code = new InlineTypeScriptCode('let : d ===== 1');
code.bind(new Stack());
}).toThrowError('Failed to transform InlineCode');

expect(console.error).toBeCalledWith(expect.stringContaining('Unexpected "=="'));

expect(mocked(printBuildMessages)).toHaveBeenCalledTimes(1);
console.error = originalConsole;
});
});

Expand Down Expand Up @@ -156,4 +158,38 @@ describe('using transformerProps', () => {
expect(mockLogger).toHaveBeenCalledWith('dummy-binary');
});
});


describe('with logLevel', () => {
describe('not provided', () => {
it('should default to "warning"', () => {
const transformFn = jest.fn(transformSync);
const code = new InlineJavaScriptCode("const fruit = 'banana';", {
transformFn,
});
code.bind(new Stack());

expect(transformFn).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
logLevel: 'warning',
}));
});
});

describe('provided', () => {
it('should use the provided logLevel', () => {
const transformFn = jest.fn(transformSync);
const code = new InlineJavaScriptCode("const fruit = 'banana';", {
transformFn,
transformOptions: {
logLevel: 'silent',
},
});
code.bind(new Stack());

expect(transformFn).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({
logLevel: 'silent',
}));
});
});
});
});

0 comments on commit 2596368

Please sign in to comment.