diff --git a/tools/@aws-cdk/cdk-build-tools/config/ext.js b/tools/@aws-cdk/cdk-build-tools/config/ext.js new file mode 100644 index 0000000000000..4c5ae2090c8eb --- /dev/null +++ b/tools/@aws-cdk/cdk-build-tools/config/ext.js @@ -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'; \ No newline at end of file diff --git a/tools/@aws-cdk/cdk-build-tools/config/jest.config.js b/tools/@aws-cdk/cdk-build-tools/config/jest.config.js index 0c47026702afe..8a8d4f048cb79 100644 --- a/tools/@aws-cdk/cdk-build-tools/config/jest.config.js +++ b/tools/@aws-cdk/cdk-build-tools/config/jest.config.js @@ -1,3 +1,10 @@ +// 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 ?? {}) { @@ -5,7 +12,7 @@ if ('aws-cdk-lib' in thisPackagesPackageJson.devDependencies ?? {}) { 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 = { @@ -15,7 +22,7 @@ module.exports = { 'ts', 'js', ], - testMatch: ['/test/**/?(*.)+(test).ts'], + testMatch: [`/test/**/?(*.)+(test).${ext}`], // Transform TypeScript using ts-jest. Use of this preset still requires the depending // package to depend on `ts-jest` directly. @@ -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`, }; diff --git a/tools/@aws-cdk/cdk-build-tools/config/snapshot-resolver.js b/tools/@aws-cdk/cdk-build-tools/config/snapshot-resolver.js new file mode 100644 index 0000000000000..7c6061e2918ed --- /dev/null +++ b/tools/@aws-cdk/cdk-build-tools/config/snapshot-resolver.js @@ -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}`, +}; \ No newline at end of file diff --git a/tools/@aws-cdk/construct-metadata-updater/test/metadata-updater.test.ts b/tools/@aws-cdk/construct-metadata-updater/test/metadata-updater.test.ts index 018622975c1bb..65f4639e3cac7 100644 --- a/tools/@aws-cdk/construct-metadata-updater/test/metadata-updater.test.ts +++ b/tools/@aws-cdk/construct-metadata-updater/test/metadata-updater.test.ts @@ -1,8 +1,3 @@ -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 @@ -10,6 +5,11 @@ 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;