Skip to content

Commit

Permalink
fix: use start:local only if set (#941)
Browse files Browse the repository at this point in the history
  • Loading branch information
agerard-godaddy authored Oct 9, 2024
1 parent dae7433 commit 69ab069
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 80 deletions.
2 changes: 1 addition & 1 deletion packages/gasket-plugin-cypress/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ will automatically add the following `scripts` to your `package.json`:

- Before running `npm run cypress` or `npm run cypress:headless`, make sure the
server is running in a separate terminal tab. You can start the server by
running `npm run start:local` or `next start`.
running `npm run start` (or `npm run start:local`).
- Before starting the server for the first time, you must run
`npm run build` to ensure that all necessary assets are built and available
for testing.
Expand Down
7 changes: 4 additions & 3 deletions packages/gasket-plugin-cypress/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ const plugin = {
});
}

const startCmd = pkg.has('scripts', 'start:local') ? 'start:local' : 'start';

pkg.add('scripts', {
'start:local': 'next start',
'cypress': 'cypress open',
'cypress:headless': 'cypress run',
'e2e': `start-server-and-test start:local ${LOCAL_SERVER_URL} cypress`,
'e2e': `start-server-and-test ${startCmd} ${LOCAL_SERVER_URL} cypress`,
'e2e:headless':
`start-server-and-test start:local ${LOCAL_SERVER_URL} cypress:headless`
`start-server-and-test ${startCmd} ${LOCAL_SERVER_URL} cypress:headless`
});
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/gasket-plugin-cypress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"lint": "eslint .",
"lint:fix": "npm run lint -- --fix",
"test": "cross-env NODE_OPTIONS='--unhandled-rejections=strict' jest",
"test:watch": "jest --watchAll",
"test:coverage": "jest --coverage",
"test:watch": "npm run test -- --watchAll",
"test:coverage": "npm run test -- --coverage",
"posttest": "npm run lint && npm run typecheck",
"typecheck": "tsc",
"typecheck:watch": "tsc --watch"
Expand Down
140 changes: 66 additions & 74 deletions packages/gasket-plugin-cypress/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,36 @@
const self = require('../package.json');
const plugin = require('../lib/index.js');
const { name, version, description } = require('../package');
const createHook = plugin.hooks.create.handler;

describe('Plugin', function () {
/**
* Create a new project
* @returns {Promise<object>} project
*/
async function create() {
const pkg = {};

await plugin.hooks.create.handler(
{},
{
pkg: {
add: (key, value) => {
pkg[key] = pkg[key] || {};
pkg[key] = { ...pkg[key], ...value };
},
has: (key, value) => !!pkg[key] && !!pkg[key][value]
}
}
);

return { pkg };
}

/**
* Create a new React project
* @returns {Promise<object>} project
*/
async function createReact() {
const files = [];
const pkg = {
dependencies: {
react: '1.0.0'
}
let gasket, createContext;
let ctxPkg, ctxFiles;
const addReact = () => {
ctxPkg.dependencies = {
react: '1.0.0'
};

await plugin.hooks.create.handler(
{},
{
pkg: {
add: (key, value) => {
pkg[key] = pkg[key] || {};
pkg[key] = { ...pkg[key], ...value };
},
has: (key, value) => !!pkg[key] && !!pkg[key][value]
};

beforeEach(() => {
ctxPkg = {};
ctxFiles = [];
gasket = {};
createContext = {
pkg: {
add: (key, value) => {
ctxPkg[key] = ctxPkg[key] ?? {};
ctxPkg[key] = { ...ctxPkg[key], ...value };
},
files: {
add: (...args) => {
files.push(...args);
}
has: (key, value) => !!ctxPkg[key] && !!ctxPkg[key][value]
},
files: {
add: (...args) => {
ctxFiles.push(...args);
}
}
);

return {
files,
pkg
};
}
});

it('is an object', function () {
expect(plugin).toBeInstanceOf(Object);
Expand Down Expand Up @@ -88,68 +58,90 @@ describe('Plugin', function () {

describe('react', function () {
it('includes a glob for the `generator/cypress.config.js` contents for react projects', async function () {
const { files } = await createReact();
addReact();
await createHook(gasket, createContext);

expect(files[0]).toContain('/../generator/*');
expect(files[1]).toContain('/../generator/**/*');
expect(ctxFiles[0]).toContain('/../generator/*');
expect(ctxFiles[1]).toContain('/../generator/**/*');
});
});

describe('adds react specific dependencies', function () {
['cypress', 'start-server-and-test'].forEach((name) => {
it(`adds "${name}" in the devDependencies`, async function () {
const { pkg } = await createReact();
addReact();
await createHook(gasket, createContext);

expect(pkg.devDependencies).toHaveProperty(name);
expect(ctxPkg.devDependencies).toHaveProperty(name);
});
});

it('depends on the same versions', async function () {
const { pkg } = await createReact();
addReact();
await createHook(gasket, createContext);

expect(typeof pkg.devDependencies).toBe('object');
Object.keys(pkg.devDependencies).forEach((key) => {
expect(typeof ctxPkg.devDependencies).toBe('object');
Object.keys(ctxPkg.devDependencies).forEach((key) => {
expect(self.devDependencies).toHaveProperty(key);
expect(self.devDependencies[key]).toEqual(pkg.devDependencies[key]);
expect(self.devDependencies[key]).toEqual(ctxPkg.devDependencies[key]);
});
});
});

describe('dependencies', function () {
it('adds "cypress" in the devDependencies', async function () {
const { pkg } = await create();
await createHook(gasket, createContext);

expect(pkg.devDependencies).toHaveProperty('cypress');
expect(ctxPkg.devDependencies).toHaveProperty('cypress');
});

it('depends on the same versions', async function () {
const { pkg } = await create();
await createHook(gasket, createContext);

expect(typeof pkg.devDependencies).toBe('object');
Object.keys(pkg.devDependencies).forEach((key) => {
expect(typeof ctxPkg.devDependencies).toBe('object');
Object.keys(ctxPkg.devDependencies).forEach((key) => {
expect(self.devDependencies).toHaveProperty(key);
expect(self.devDependencies[key]).toEqual(pkg.devDependencies[key]);
expect(self.devDependencies[key]).toEqual(ctxPkg.devDependencies[key]);
});
});
});

describe('scripts', function () {
it('uses the same scrips in our package.json', async function () {
const { pkg } = await create();
it('add expected scripts', async function () {
await createHook(gasket, createContext);

const expected = {
'cypress': 'cypress open',
'cypress:headless': 'cypress run',
'e2e': 'start-server-and-test start http://localhost:3000 cypress',
'e2e:headless':
'start-server-and-test start http://localhost:3000 cypress:headless'
};

expect(typeof ctxPkg.scripts).toBe('object');
Object.keys(ctxPkg.scripts).forEach((key) => {
expect(expected).toHaveProperty(key);
expect(expected[key]).toEqual(ctxPkg.scripts[key]);
});
});

it('use start:local script if set', async function () {
ctxPkg.scripts = { 'start:local': 'GASKET_ENV=local next start' };
await createHook(gasket, createContext);

const expected = {
'start:local': 'next start',
'start:local': 'GASKET_ENV=local next start',
'cypress': 'cypress open',
'cypress:headless': 'cypress run',
'e2e': 'start-server-and-test start:local http://localhost:3000 cypress',
'e2e:headless':
'start-server-and-test start:local http://localhost:3000 cypress:headless'
};

expect(typeof pkg.scripts).toBe('object');
Object.keys(pkg.scripts).forEach((key) => {
expect(typeof ctxPkg.scripts).toBe('object');
Object.keys(ctxPkg.scripts).forEach((key) => {
expect(expected).toHaveProperty(key);
expect(expected[key]).toEqual(pkg.scripts[key]);
expect(expected[key]).toEqual(ctxPkg.scripts[key]);
});
});
});
Expand Down

0 comments on commit 69ab069

Please sign in to comment.