Skip to content

Commit

Permalink
feat(testing): add regex based copy files & unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Troy Houston authored and vsavkin committed Jun 4, 2019
1 parent 7a161ae commit 609a2f6
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 2 deletions.
60 changes: 59 additions & 1 deletion packages/cypress/src/builders/cypress/cypress.impl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ describe('Cypress builder', () => {
cypressRun = spyOn(Cypress, 'run').and.returnValue(Promise.resolve({}));
cypressOpen = spyOn(Cypress, 'open').and.returnValue(Promise.resolve({}));
cypressConfig = {
fixturesFolder: '../../dist/out-tsc/apps/my-app-e2e/src/fixtures'
fixturesFolder: '../../dist/out-tsc/apps/my-app-e2e/src/fixtures',
integrationFolder: '../../dist/out-tsc/apps/my-app-e2e/src/integration'
};
spyOn(fsUtility, 'readJsonFile').and.callFake(path => {
return path.endsWith('tsconfig.json')
Expand Down Expand Up @@ -224,4 +225,61 @@ describe('Cypress builder', () => {

fakeEventEmitter.emit('exit'); // Passing tsc command
});

it('should copy regex files to out-dir', async done => {
const regex: string = '^.+\\.feature$';

const run = await architect.scheduleBuilder('@nrwl/cypress:cypress', {
...cypressBuilderOptions,
copyFiles: regex
});
run.result.then(async () => {
await run.stop();
expect(fsExtras.copySync).toHaveBeenCalledWith(
'/root/apps/my-app-e2e/src/integration',
'/root/dist/out-tsc/apps/my-app-e2e/src/integration',
{ filter: jasmine.any(Function) }
);
done();
});

fakeEventEmitter.emit('exit'); // Passing tsc command
});

it('should not copy regex files if the regex is not defined', async done => {
const regex: string = undefined;

const run = await architect.scheduleBuilder('@nrwl/cypress:cypress', {
...cypressBuilderOptions,
copyFiles: regex
});
run.result.then(async () => {
await run.stop();
expect(fsExtras.copySync).not.toHaveBeenCalledWith(
'/root/apps/my-app-e2e/src/integration',
'/root/dist/out-tsc/apps/my-app-e2e/src/integration',
{ filter: jasmine.any(Function) }
);
done();
});

fakeEventEmitter.emit('exit'); // Passing tsc command
});

it('should not copy regex files if the integration files are not defined in the cypress config', async done => {
cypressConfig = {};
const regex: string = '^.+\\.feature$';

const run = await architect.scheduleBuilder('@nrwl/cypress:cypress', {
...cypressBuilderOptions,
copyFiles: regex
});
run.result.then(async () => {
await run.stop();
expect(fsExtras.copySync).not.toHaveBeenCalled();
done();
});

fakeEventEmitter.emit('exit'); // Passing tsc command
});
});
42 changes: 41 additions & 1 deletion packages/cypress/src/builders/cypress/cypress.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface CypressBuilderOptions extends JsonObject {
browser?: string;
env?: Record<string, string>;
spec?: string;
copyFiles?: string;
}

try {
Expand Down Expand Up @@ -62,7 +63,14 @@ function run(
);

return compileTypescriptFiles(options.tsConfig, options.watch, context).pipe(
tap(() => copyCypressFixtures(options.cypressConfig, context)),
tap(() => {
copyCypressFixtures(options.cypressConfig, context);
copyIntegrationFilesByRegex(
options.cypressConfig,
context,
options.copyFiles
);
}),
concatMap(() =>
options.devServerTarget
? startDevServer(options.devServerTarget, options.watch, context).pipe(
Expand Down Expand Up @@ -168,6 +176,38 @@ function copyCypressFixtures(
);
}

/**
* @whatItDoes Copy all the integration files that match the given regex into the dist folder.
* This is done because `tsc` doesn't handle all file types, e.g. Cucumbers `feature` files.
* @param fileExtension File extension to copy
*/
function copyIntegrationFilesByRegex(
cypressConfigPath: string,
context: BuilderContext,
regex: string
) {
const cypressConfig = readJsonFile(
path.join(context.workspaceRoot, cypressConfigPath)
);

if (!regex || !cypressConfig.integrationFolder) {
return;
}

const regExp: RegExp = new RegExp(regex);

copySync(
`${path.dirname(
path.join(context.workspaceRoot, cypressConfigPath)
)}/src/integration`,
path.join(
path.dirname(path.join(context.workspaceRoot, cypressConfigPath)),
cypressConfig.integrationFolder
),
{ filter: file => regExp.test(file) }
);
}

/**
* @whatItDoes Initialize the Cypress test runner with the provided project configuration.
* If `headless` is `false`: open the Cypress application, the user will
Expand Down
4 changes: 4 additions & 0 deletions packages/cypress/src/builders/cypress/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
"spec": {
"type": "string",
"description": "A comma delimited glob string that is provided to the Cypress runner to specify which spec files to run. i.e. '**examples/**,**actions.spec**"
},
"copyFiles": {
"type": "string",
"description": "A regex string that is used to choose what additional integration files to copy to the dist folder"
}
},
"additionalProperties": false,
Expand Down

0 comments on commit 609a2f6

Please sign in to comment.