diff --git a/.gitignore b/.gitignore index b0825a0..6d387cd 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ cypress/videos/ cypress/screenshots .nyc_output coverage +*.tgz diff --git a/README.md b/README.md index 3cf58ce..d5ca077 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ > A little helper to unit test Vue components in the open source [Cypress.io](https://www.cypress.io/) E2E test runner **v4.5.0+** -**Jump to:** [Comparison](#comparison), [Blog posts](#blog-posts), Examples: [basic](#basic-examples), [advanced](#advanced-examples), [external](#external-examples), [Code coverage](#code-coverage) +**Jump to:** [Comparison](#comparison), [Blog posts](#blog-posts), Examples: [basic](#basic-examples), [advanced](#advanced-examples), [full](#full-examples), [external](#external-examples), [Code coverage](#code-coverage) ## TLDR @@ -598,6 +598,16 @@ Spec | Description [mocking-imports](cypress/component/advanced/mocking-imports) | Stub ES6 imports from the tests +### Full examples + +We have several subfolders in [examples](examples) folder. + + +Folder Name | Description +--- | --- +[cli](examples/cli) | An example app scaffolded using Vue CLI and the component testing added using `vue add cypress-experimental` command. + + ### External examples diff --git a/circle.yml b/circle.yml index f23888b..f25e031 100644 --- a/circle.yml +++ b/circle.yml @@ -11,7 +11,20 @@ workflows: - cypress/install: name: Install executor: cypress/base-12 - build: npm run build + # creates files in "dist" folder + build: | + npm run build + echo "" + echo "Build package archive πŸ“¦" + echo "" + # first show the contents to be packed + npm pack --dry + echo "" + echo "packing ..." + echo "" + npm pack + echo "" + ls -la post-steps: - run: name: Show info πŸ“Ί @@ -40,6 +53,41 @@ workflows: echo "Test files for this machine are $TESTFILES" npx cypress run --spec $TESTFILES + - cypress/run: + name: Example CLI + executor: cypress/base-12 + requires: + - Install + install-command: npm install + verify-command: echo 'Already verified' + no-workspace: true + working_directory: examples/cli + command: | + echo "🦢🏻 Scaffolding component tests" + echo "" + # scaffolds Babel + Webpack combo + npx --package @vue/cli vue add cypress-experimental + echo "" + echo "🦢🏻 test scaffolded project, should work" + echo "" + DEBUG=cypress-vue-unit-test npx cypress run --spec 'tests/components/**/*.js' + # let's inspect the scaffolded app + ls -la + echo "" + echo "🦢🏻 install the current cypress-vue-unit-test" + echo "" + npm install ../../cypress-vue-unit-test-0.0.0-development.tgz + echo "" + echo "🦢🏻 run component tests" + echo "" + DEBUG=cypress-vue-unit-test npx cypress run --spec 'tests/components/**/*.js' + echo "" + echo "🦢🏻 look at the generated files, should have coverage" + # (after updating cypress-experimental) + echo "" + ls -la + store_artifacts: true + # this job attaches the workspace left by the install job # so it is ready to run Cypress tests # only we will run semantic release script instead @@ -55,6 +103,7 @@ workflows: requires: - Install - Test + - Example CLI install-command: echo 'Already installed' verify-command: echo 'Already verified' no-workspace: true diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js index 377200e..3fc2c04 100644 --- a/cypress/plugins/index.js +++ b/cypress/plugins/index.js @@ -1,22 +1,6 @@ -// default webpack file preprocessor is good for simple cases -const { onFileDefaultPreprocessor } = require('../../preprocessor/webpack') - +/// +const preprocessor = require('../../plugins/webpack') module.exports = (on, config) => { - require('@cypress/code-coverage/task')(on, config) - on('file:preprocessor', onFileDefaultPreprocessor(config)) - - // IMPORTANT to return the config object - // with the any changed environment variables + preprocessor(on, config) return config } - -/* - for more complex cases, when the project already includes webpack.config.js - - const { - onFilePreprocessor - } = require('cypress-vue-unit-test/preprocessor/webpack') - module.exports = on => { - on('file:preprocessor', onFilePreprocessor('../path/to/webpack.config')) - } -*/ diff --git a/cypress/support/index.js b/cypress/support/index.js index 64de925..a73d518 100644 --- a/cypress/support/index.js +++ b/cypress/support/index.js @@ -1,8 +1 @@ -require('@cypress/code-coverage/support') - -beforeEach(() => { - const container = document.getElementById('cypress-jsdom') - if (container) { - container.innerHTML = '' - } -}) +require('../../dist/support') diff --git a/docs/manual-install.md b/docs/manual-install.md index 4399750..0355117 100644 --- a/docs/manual-install.md +++ b/docs/manual-install.md @@ -8,19 +8,18 @@ npm install -D cypress cypress-vue-unit-test ``` -2. Include this plugin `cypress/plugin/index.js` +2. Include this plugin from your project's `cypress/plugin/index.js` file ```js -// default webpack file preprocessor is good for simple cases -// Required to temporarily patch async components, chunking, and inline image loading -import { onFileDefaultPreprocessor } from 'cypress-vue-unit-test/dist/preprocessor/webpack' - -module.exports = (on) => { - on('file:preprocessor', onFileDefaultPreprocessor) +const preprocessor = require('cypress-vue-unit-test/dist/plugins/webpack') +module.exports = (on, config) => { + preprocessor(on, config) + // IMPORTANT return the config object + return config } ``` -3. Include the support file `cypress/support/index.js` +3. Include the support file from your project's `cypress/support/index.js` file ```js import 'cypress-vue-unit-test/dist/support' diff --git a/examples/cli/.gitignore b/examples/cli/.gitignore new file mode 100644 index 0000000..11f5d71 --- /dev/null +++ b/examples/cli/.gitignore @@ -0,0 +1,22 @@ +.DS_Store +node_modules +/dist + +# local env files +.env.local +.env.*.local + +# Log files +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* + +# Editor directories and files +.idea +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/examples/cli/.npmrc b/examples/cli/.npmrc new file mode 100644 index 0000000..43c97e7 --- /dev/null +++ b/examples/cli/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/examples/cli/README.md b/examples/cli/README.md new file mode 100644 index 0000000..29f9f46 --- /dev/null +++ b/examples/cli/README.md @@ -0,0 +1,3 @@ +# cli-example + +Application scaffolded with Vue CLI v3 diff --git a/examples/cli/babel.config.js b/examples/cli/babel.config.js new file mode 100644 index 0000000..e955840 --- /dev/null +++ b/examples/cli/babel.config.js @@ -0,0 +1,5 @@ +module.exports = { + presets: [ + '@vue/cli-plugin-babel/preset' + ] +} diff --git a/examples/cli/package.json b/examples/cli/package.json new file mode 100644 index 0000000..6017018 --- /dev/null +++ b/examples/cli/package.json @@ -0,0 +1,42 @@ +{ + "name": "cli-example", + "version": "0.1.0", + "private": true, + "scripts": { + "serve": "vue-cli-service serve", + "build": "vue-cli-service build", + "lint": "vue-cli-service lint" + }, + "dependencies": { + "core-js": "^3.6.5", + "vue": "^2.6.11" + }, + "devDependencies": { + "@vue/cli-plugin-babel": "~4.4.0", + "@vue/cli-plugin-eslint": "~4.4.0", + "@vue/cli-service": "~4.4.0", + "babel-eslint": "^10.1.0", + "eslint": "^6.7.2", + "eslint-plugin-vue": "^6.2.2", + "vue-template-compiler": "^2.6.11" + }, + "eslintConfig": { + "root": true, + "env": { + "node": true + }, + "extends": [ + "plugin:vue/essential", + "eslint:recommended" + ], + "parserOptions": { + "parser": "babel-eslint" + }, + "rules": {} + }, + "browserslist": [ + "> 1%", + "last 2 versions", + "not dead" + ] +} diff --git a/examples/cli/src/App.vue b/examples/cli/src/App.vue new file mode 100644 index 0000000..55df315 --- /dev/null +++ b/examples/cli/src/App.vue @@ -0,0 +1,28 @@ + + + + + diff --git a/examples/cli/src/assets/logo.png b/examples/cli/src/assets/logo.png new file mode 100644 index 0000000..f3d2503 Binary files /dev/null and b/examples/cli/src/assets/logo.png differ diff --git a/examples/cli/src/components/HelloWorld.vue b/examples/cli/src/components/HelloWorld.vue new file mode 100644 index 0000000..879051a --- /dev/null +++ b/examples/cli/src/components/HelloWorld.vue @@ -0,0 +1,58 @@ + + + + + + diff --git a/examples/cli/src/main.js b/examples/cli/src/main.js new file mode 100644 index 0000000..63eb05f --- /dev/null +++ b/examples/cli/src/main.js @@ -0,0 +1,8 @@ +import Vue from 'vue' +import App from './App.vue' + +Vue.config.productionTip = false + +new Vue({ + render: h => h(App), +}).$mount('#app') diff --git a/package.json b/package.json index 15d659e..f5c4333 100644 --- a/package.json +++ b/package.json @@ -25,15 +25,15 @@ "url": "https://github.com/bahmutov/cypress-vue-unit-test.git" }, "scripts": { - "build": "babel src --ignore src/*spec.js --out-dir dist && babel preprocessor --out-dir dist/preprocessor", + "build": "babel src --ignore src/*spec.js --out-dir dist && babel preprocessor --out-dir dist/preprocessor && babel plugins --out-dir dist/plugins", "ban": "ban", "deps": "echo skip deps-ok && dependency-check --no-dev .", "issues": "git-issues", "license": "license-checker --production --onlyunknown --csv", - "lint": "prettier --check '*.js' 'cypress/**/*.js' 'src/**/*.js' 'preprocessor/**/*.js'", + "lint": "prettier --check '*.js' 'cypress/**/*.js' 'src/**/*.js' 'preprocessor/**/*.js' 'plugins/**/*.js'", "prelint": "npm run pretty", "pretest": "npm run lint && npm run build", - "pretty": "prettier --write '*.js' 'cypress/**/*.js' 'src/**/*.js' 'preprocessor/**/*.js'", + "pretty": "prettier --write '*.js' 'cypress/**/*.js' 'src/**/*.js' 'preprocessor/**/*.js' 'plugins/**/*.js'", "size": "npm pack --dry", "test": "npm run cy:run", "unit": "mocha src/*-spec.js", diff --git a/plugins/webpack/index.js b/plugins/webpack/index.js new file mode 100644 index 0000000..aab6e71 --- /dev/null +++ b/plugins/webpack/index.js @@ -0,0 +1,25 @@ +/// +const { onFileDefaultPreprocessor } = require('../../preprocessor/webpack') + +/** + * Registers Cypress preprocessor for Vue component testing. + * IMPORTANT to return the config object with + * with the any changed environment variables. + * + * @param {Cypress.PluginConfigOptions} config Cypress config object. + * @example + * // in your project's plugins/index.js file + * const preprocessor = require('cypress-vue-unit-test/dist/plugins/webpack') + * module.exports = (on, config) => { + * preprocessor(on, config) + * // IMPORTANT return the config object + * return config + * } + */ +const cypressPluginsFn = (on, config) => { + require('@cypress/code-coverage/task')(on, config) + on('file:preprocessor', onFileDefaultPreprocessor(config)) + return config +} + +module.exports = cypressPluginsFn diff --git a/src/support.js b/src/support.js index c7970a7..25c5a6a 100644 --- a/src/support.js +++ b/src/support.js @@ -1,9 +1,12 @@ /* eslint-env mocha */ +require('@cypress/code-coverage/support') + /** Initialize an empty document with root element */ function renderTestingPlatform() { const document = cy.state('document') const el = document.getElementById('cypress-jsdom') if (el) { + // clean the element before each test while (el.hasChildNodes()) { el.removeChild(el.lastChild) } diff --git a/webpack.config.js b/webpack.config.js index dc5c096..e86a028 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -7,7 +7,8 @@ module.exports = { resolve: { extensions: ['.js', '.json', '.vue'], alias: { - 'cypress-vue-unit-test': path.join(__dirname, 'src'), + // point at the built file + 'cypress-vue-unit-test': path.join(__dirname, 'dist'), }, }, module: {