Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tools/@aws-cdk/cdk-build-tools/config/ext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// In a separate file because this logic needs to be shared between files
//
// On developer boxes we want to run the .ts files directly for quickest
// iteration (save -> run), but on CI machines we want to run the compiled
// JavaScript for highest throughput.

const isCi = !!process.env.CI || !!process.env.CODEBUILD_BUILD_ID;
module.exports = isCi ? 'js' : 'ts';
16 changes: 14 additions & 2 deletions tools/@aws-cdk/cdk-build-tools/config/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// Crazy stuff!
//
// On developer boxes we want to run the .ts files directly for quickest
// iteration (save -> run), but on CI machines we want to run the compiled
// JavaScript for highest throughput.
const ext = require('./ext');

const thisPackagesPackageJson = require(`${process.cwd()}/package.json`);
const setupFilesAfterEnv = [];
if ('aws-cdk-lib' in thisPackagesPackageJson.devDependencies ?? {}) {
// If we depend on aws-cdk-lib, use the provided autoclean hook
setupFilesAfterEnv.push('aws-cdk-lib/testhelpers/jest-autoclean');
} else if (thisPackagesPackageJson.name === 'aws-cdk-lib') {
// If we *ARE* aws-cdk-lib, use the hook in a slightly different way
setupFilesAfterEnv.push('./testhelpers/jest-autoclean.ts');
setupFilesAfterEnv.push(`./testhelpers/jest-autoclean.${ext}`);
}

module.exports = {
Expand All @@ -15,7 +22,7 @@ module.exports = {
'ts',
'js',
],
testMatch: ['<rootDir>/test/**/?(*.)+(test).ts'],
testMatch: [`<rootDir>/test/**/?(*.)+(test).${ext}`],

// Transform TypeScript using ts-jest. Use of this preset still requires the depending
// package to depend on `ts-jest` directly.
Expand Down Expand Up @@ -43,4 +50,9 @@ module.exports = {
reporters: ['default', ['jest-junit', { suiteName: 'jest tests', outputDirectory: 'coverage' }]],

setupFilesAfterEnv,

// A consequence of doing this is that snapshots files are always named after
// the currently executing file, which will be different for .ts and .js
// extensions, so we need to do some more work to redirect always to .ts
snapshotResolver: `${__dirname}/snapshot-resolver.js`,
};
20 changes: 20 additions & 0 deletions tools/@aws-cdk/cdk-build-tools/config/snapshot-resolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path');
const ext = require('./ext');

const dotext = `.${ext}`;

module.exports = {
// resolves from test to snapshot path
resolveSnapshotPath: (testPath, snapshotExtension) => {
return `${path.dirname(testPath)}/__snapshots__/${path.basename(testPath, dotext)}.ts${snapshotExtension}`;
},

// resolves from snapshot to test path
resolveTestPath: (snapshotFilePath, snapshotExtension) => {
const testDir = path.dirname(path.dirname(snapshotFilePath));
return `${testDir}/${path.basename(snapshotFilePath, `.ts${snapshotExtension}`)}${dotext}`;
},

// Example test path, used for preflight consistency check of the implementation above
testPathForConsistencyCheck: `test/apple.test.${dotext}`,
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ConstructsUpdater, EnumLikeUpdater, PropertyUpdater } from '../lib/metadata-updater';
import { Project, ClassDeclaration, SourceFile, QuoteKind, IndentationText } from 'ts-morph';
import * as path from 'path';
import * as fs from 'fs';

// Mock ts-morph
jest.mock('ts-morph');
// Mock fs
jest.mock('fs');
// Mock path
jest.mock('path');

import { ConstructsUpdater, EnumLikeUpdater, PropertyUpdater } from '../lib/metadata-updater';
import { Project, ClassDeclaration, SourceFile, QuoteKind, IndentationText } from 'ts-morph';
import * as path from 'path';
import * as fs from 'fs';

describe('ResourceMetadataUpdater', () => {
let updater: ConstructsUpdater;
let mockSourceFile: jest.Mocked<SourceFile>;
Expand Down