Skip to content

Commit 5473a01

Browse files
authored
Rerun cypress tests (#164)
* logout before createSimulation * more logging * logging in spec * always delete simulation in logout * constant for cypress simulation key * add changeset
1 parent 9b9d82f commit 5473a01

File tree

9 files changed

+63
-47
lines changed

9 files changed

+63
-47
lines changed

.changes/rerun-cypress-tests.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
"@simulacrum/auth0-cypress": minor
3+
---
4+
Destroy simulation before creating a new one

integrations/cypress/cypress.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
"baseUrl": "http://localhost:3000",
33
"video": false,
44
"screenshotOnRunFailure": false,
5-
"defaultCommandTimeout": 20000,
65
"fixturesFolder": "cypress/fixtures",
76
"integrationFolder": "cypress/integration",
87
"pluginsFile": "cypress/plugins/index.js",
98
"screenshotsFolder": "cypress/screenshots",
109
"videosFolder": "cypress/videos",
1110
"supportFile": "cypress/support/index.ts",
1211
"configFile": "cypress/tsconfig.json",
13-
"chromeWebSecurity": false,
14-
"execTimeout": 10000
12+
"chromeWebSecurity": false
1513
}

integrations/cypress/cypress/integration/login.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ describe("auth", () => {
3333
});
3434
});
3535

36-
3736
describe("createSimulation in beforeEach and logout in afterEach", () => {
3837
beforeEach(() => {
3938
cy.createSimulation(auth0Config);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const SimulationId = 'cypress';
Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Slice } from '@effection/atom';
22
import { CreateSimulation, GetClientFromSpec, TestState } from '../types';
33
import { makeCypressLogger } from '../utils/cypress-logger';
4+
import { SimulationId } from './constants';
45

56
export interface MakeCreateSimulationOptions {
67
atom: Slice<TestState>;
@@ -10,7 +11,7 @@ export interface MakeCreateSimulationOptions {
1011
const log = makeCypressLogger('simulacrum-create-simulation');
1112

1213
export const makeCreateSimulation = ({ atom, getClientFromSpec }: MakeCreateSimulationOptions) => (options: CreateSimulation) => {
13-
return new Cypress.Promise((resolve, reject) => {
14+
return cy.logout().then(() => {
1415
let client = getClientFromSpec(Cypress.spec.name);
1516

1617
let { debug = false, domain, client_id, ...auth0Options } = options;
@@ -19,35 +20,37 @@ export const makeCreateSimulation = ({ atom, getClientFromSpec }: MakeCreateSimu
1920

2021
let port = Number(domain.split(':').slice(-1)[0]);
2122

22-
assert(typeof client !== 'undefined', 'no client created in createSimulation');
23+
log(`creating simulation with options: ${JSON.stringify(options)}`);
2324

24-
client.createSimulation("auth0", {
25-
options: {
26-
...auth0Options,
27-
clientId: client_id,
28-
},
29-
services: {
30-
auth0: {
31-
port,
25+
return new Cypress.Promise((resolve, reject) => {
26+
client.createSimulation("auth0", {
27+
options: {
28+
...auth0Options,
29+
clientId: client_id,
3230
},
33-
},
34-
debug,
35-
key: 'cypress'
36-
}).then(simulation => {
37-
atom.slice(Cypress.spec.name).update(current => {
38-
return {
39-
...current,
40-
simulation
41-
};
31+
services: {
32+
auth0: {
33+
port,
34+
},
35+
},
36+
debug,
37+
key: SimulationId
38+
}).then(simulation => {
39+
atom.slice(Cypress.spec.name).update(current => {
40+
return {
41+
...current,
42+
simulation
43+
};
44+
});
45+
46+
log(`sumalation created ${JSON.stringify(simulation)}`);
47+
48+
resolve(simulation);
49+
}).catch((e) => {
50+
log(`create-simulation failed ${e.message}`);
51+
52+
reject(e);
4253
});
43-
44-
log(`sumalation created ${JSON.stringify(simulation)}`);
45-
46-
resolve(simulation);
47-
}).catch((e) => {
48-
log(`create-simulation failed ${e.message}`);
49-
50-
reject(e);
5154
});
5255
});
5356
};

integrations/cypress/cypress/support/commands/given.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,24 @@ const log = makeCypressLogger('simulacrum-given');
1313
export const makeGiven = ({ atom, getClientFromSpec }: MakeGivenOptions) => (attrs: Partial<Person> = {}) => {
1414
return new Cypress.Promise((resolve, reject) => {
1515
let client = getClientFromSpec(Cypress.spec.name);
16+
1617
let simulation = atom.slice(Cypress.spec.name, 'simulation').get();
1718

1819
assert(!!simulation, 'no sumulation in given');
19-
assert(!!client && typeof client.given === 'function', 'no valid client in given');
20+
21+
log(`creating person with attrs: ${JSON.stringify(attrs)}`);
2022

2123
client.given<Person>(simulation, "person", attrs)
2224
.then((scenario) => {
25+
log(`person created ${JSON.stringify(scenario)}`);
26+
2327
atom.slice(Cypress.spec.name).update(current => {
2428
return {
2529
...current,
2630
person: scenario.data
2731
};
2832
});
2933

30-
log(`scenario created with ${JSON.stringify(scenario)}`);
31-
3234
resolve(scenario.data);
3335
})
3436
.catch((e) => {

integrations/cypress/cypress/support/commands/logout.ts

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { Slice } from '@effection/atom';
2+
import { Simulation } from '@simulacrum/client';
23
import { GetClientFromSpec, TestState } from '../types';
34
import { makeCypressLogger } from '../utils/cypress-logger';
5+
import { SimulationId } from './constants';
46

57
export interface MakeLogoutOptions {
68
atom: Slice<TestState>;
@@ -11,23 +13,14 @@ const log = makeCypressLogger('simulacrum-logout');
1113

1214
export const makeLogout = ({ atom, getClientFromSpec }: MakeLogoutOptions) => () => {
1315
return new Cypress.Promise((resolve, reject) => {
14-
let client = getClientFromSpec(Cypress.spec.name);
15-
1616
log('in logout');
1717

18-
let simulation = atom.slice(Cypress.spec.name, 'simulation').get();
19-
20-
if(!client || !simulation) {
21-
log('no simulation or client');
22-
resolve();
23-
return;
24-
}
18+
let client = getClientFromSpec(Cypress.spec.name);
2519

26-
client.destroySimulation(simulation).then(() => {
20+
client.destroySimulation({ id: SimulationId } as Simulation).then(() => {
2721
log('simulation destroyed');
2822

2923
atom.slice(Cypress.spec.name).remove();
30-
3124
resolve();
3225
}).catch(e => {
3326
log(`logout failed with ${e.message}`);
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
import { Slice } from '@effection/atom';
22
import { Client, createClient } from '@simulacrum/client';
33
import { TestState } from '../types';
4+
import { makeCypressLogger } from './cypress-logger';
45

56
export interface MakeGetClientFromSpecOptions {
67
atom: Slice<TestState>;
78
port: number;
89
}
910

11+
const log = makeCypressLogger('simulacrum-spec');
12+
1013
export const makeGetClientFromSpec = ({ atom, port }: MakeGetClientFromSpecOptions) => function getClientFromSpec (spec: string) {
1114
let client: Client;
1215

13-
if(!atom.slice(spec).get()) {
16+
if(typeof atom.slice(spec).get()?.client?.createSimulation !== 'function') {
17+
log(`creating client for ${spec}`);
1418
client = createClient(`http://localhost:${port}`);
1519
atom.set({ [spec]: { client: client } });
20+
} else {
21+
log(`client already exists for ${spec}`);
22+
client = atom.slice(spec, 'client').get();
1623
}
1724

18-
return atom.slice(spec, 'client').get();
25+
// probably not needed but....good to know
26+
assert(typeof client?.createSimulation === 'function', 'no client created in getClientFromSpec');
27+
28+
return client;
1929
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
export function combineURLs(baseURL: string, relativeURL: string): string {
3+
return relativeURL
4+
? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
5+
: baseURL;
6+
}

0 commit comments

Comments
 (0)