From 69ab069be984e4e33140f71ea5cf80c202f0d2c4 Mon Sep 17 00:00:00 2001 From: Andrew Gerard <63810935+agerard-godaddy@users.noreply.github.com> Date: Wed, 9 Oct 2024 13:43:39 -0700 Subject: [PATCH] fix: use start:local only if set (#941) --- packages/gasket-plugin-cypress/README.md | 2 +- packages/gasket-plugin-cypress/lib/index.js | 7 +- packages/gasket-plugin-cypress/package.json | 4 +- .../gasket-plugin-cypress/test/index.test.js | 140 +++++++++--------- 4 files changed, 73 insertions(+), 80 deletions(-) diff --git a/packages/gasket-plugin-cypress/README.md b/packages/gasket-plugin-cypress/README.md index 646466fa8..b9ee57633 100644 --- a/packages/gasket-plugin-cypress/README.md +++ b/packages/gasket-plugin-cypress/README.md @@ -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. diff --git a/packages/gasket-plugin-cypress/lib/index.js b/packages/gasket-plugin-cypress/lib/index.js index 065e99402..ec990763d 100644 --- a/packages/gasket-plugin-cypress/lib/index.js +++ b/packages/gasket-plugin-cypress/lib/index.js @@ -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` }); } }, diff --git a/packages/gasket-plugin-cypress/package.json b/packages/gasket-plugin-cypress/package.json index 6b1888016..0e41caeeb 100644 --- a/packages/gasket-plugin-cypress/package.json +++ b/packages/gasket-plugin-cypress/package.json @@ -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" diff --git a/packages/gasket-plugin-cypress/test/index.test.js b/packages/gasket-plugin-cypress/test/index.test.js index 1ed758b96..f8b95611a 100644 --- a/packages/gasket-plugin-cypress/test/index.test.js +++ b/packages/gasket-plugin-cypress/test/index.test.js @@ -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} 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} 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); @@ -88,57 +58,79 @@ 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', @@ -146,10 +138,10 @@ describe('Plugin', function () { '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]); }); }); });