From a0b6483a7236fc7aa6d647c8c42d7aec123abc60 Mon Sep 17 00:00:00 2001 From: Drew Powers Date: Fri, 4 Dec 2020 17:19:56 -0700 Subject: [PATCH] Improve remaining build tests --- .gitignore | 1 + CONTRIBUTING.md | 2 + package.json | 1 - .../base-url-homepage.test.js | 14 +- .../base-url-remote/base-url-remote.test.js | 14 +- test/build/base-url/base-url.test.js | 27 +- .../bugfix-named-import.test.js | 5 + test/build/build.test.js | 113 -------- test/build/cdn/cdn.test.js | 16 +- test/build/config-alias/config-alias.test.js | 120 +++++++++ .../package.json | 3 +- .../packages/css-package-a/package.json | 0 .../packages/css-package-a/style.css | 0 .../packages/css-package-b/package.json | 0 .../packages/css-package-b/style.css | 0 .../public/robots.txt | 0 .../simple-file-extension-change-plugin.js | 0 .../snowpack.config.js | 7 +- .../src/components/index.js | 0 .../src/components/style.css | 0 .../src/index.html | 0 .../src/index.js | 0 .../src/sort.js | 0 .../src/test-mjs.mjs | 0 .../config-extends-plugins.test.js | 12 +- .../config-external-package.test.js | 14 +- .../config-instantiated-object.test.js | 9 +- .../config-meta-dir/config-meta-dir.test.js | 29 ++ .../package.json | 11 +- .../public/index.js | 0 .../public/sub/index.js | 0 test/build/config-meta-dir/snowpack.config.js | 8 + test/build/config-mount/config-mount.test.js | 26 +- .../config-out-flag/config-out-flag.test.js | 5 + test/build/config-out/config-out.test.js | 5 + .../config-treeshake/config-treeshake.test.js | 21 +- test/build/config-ts-file/__snapshots__ | 12 - .../config-ts-file/config-ts-file.test.js | 17 ++ test/build/config-ts-format/__snapshots__ | 12 - test/build/config-ts-format/export.ts | 1 - test/build/config-ts-format/package.json | 12 - .../build/config-ts-format/snowpack.config.ts | 10 - test/build/config-ts-format/src/index.js | 1 - test/build/css-modules/__snapshots__ | 76 ------ test/build/css-modules/css-modules.test.js | 27 ++ .../custom-modules-dir.test.js | 12 +- .../web_modules/array-flatten.js | 24 -- .../web_modules/import-map.json | 5 - .../entrypoint-ids/entrypoint-ids.test.js | 21 +- .../html-environment-variables.test.js | 13 +- .../build/import-assets/import-assets.test.js | 43 +++ .../package.json | 2 +- test/build/import-assets/snowpack.config.json | 5 + .../src/index.js | 0 .../src/logo.png | Bin .../src/styles.css | 0 test/build/legacy-mount-scripts/__snapshots__ | 97 ------- .../legacy-mount-scripts/legacy-mount.test.js | 23 ++ test/build/legacy-mount-scripts/package.json | 10 - .../legacy-mount-scripts/snowpack.config.js | 6 + .../web_modules/array-flatten.js | 24 -- .../web_modules/import-map.json | 5 - .../meta-dir-local-baseurl/__snapshots__ | 33 --- .../build/meta-dir-local-baseurl/package.json | 26 -- .../meta-dir-remote-baseurl/__snapshots__ | 33 --- .../meta-dir-remote-baseurl/package.json | 26 -- .../meta-dir-remote-baseurl/public/index.js | 7 - .../public/sub/index.js | 7 - test/build/meta-dir/__snapshots__ | 33 --- test/build/meta-dir/public/index.js | 7 - test/build/meta-dir/public/sub/index.js | 7 - test/build/module-resolution/__snapshots__ | 66 ----- .../module-resolution.test.js | 34 +++ .../module-resolution/snowpack.config.js | 4 +- .../package-bootstrap.test.js | 12 +- .../package-tippy-js/package-tippy-js.test.js | 12 +- test/build/plugin-build-script/__snapshots__ | 15 -- .../plugin-build-script.test.js | 22 ++ .../custom-optimize-plugin.js | 0 .../package.json | 2 +- .../plugin-hook-optimize.test.js | 20 ++ .../snowpack.config.json | 0 .../src/icon.svg | 0 .../src/index.js | 0 .../src/logo.png | Bin .../src/styles.css | 0 .../custom-transform-plugin.js | 0 .../package.json | 2 +- .../plugin-hook-transform.test.js | 34 +++ .../snowpack.config.json | 1 - .../src/index.js | 0 .../src/submodule.ts | 0 test/build/plugin-optimize/.gitignore | 2 - test/build/plugin-optimize/__snapshots__ | 46 ---- test/build/plugin-run-script/.gitignore | 1 - test/build/plugin-run-script/__snapshots__ | 16 -- .../plugin-run-script.test.js | 17 ++ .../plugin-run-script/public/css/index.css | 3 + .../plugin-run-script/snowpack.config.js | 9 +- test/build/preload-css/package.json | 2 +- test/build/preload-css/preload-css.test.js | 19 +- test/build/resolve-imports/__snapshots__ | 247 ------------------ test/build/resource-proxy/__snapshots__ | 40 --- .../build/resource-proxy/snowpack.config.json | 8 - test/build/transform-sourcemap/__snapshots__ | 30 --- .../create-snowpack-app.test.js | 1 - test/setup.js | 30 --- test/test-utils.js | 23 ++ yarn.lock | 4 +- 109 files changed, 640 insertions(+), 1182 deletions(-) delete mode 100644 test/build/build.test.js create mode 100644 test/build/config-alias/config-alias.test.js rename test/build/{resolve-imports => config-alias}/package.json (88%) rename test/build/{resolve-imports => config-alias}/packages/css-package-a/package.json (100%) rename test/build/{resolve-imports => config-alias}/packages/css-package-a/style.css (100%) rename test/build/{resolve-imports => config-alias}/packages/css-package-b/package.json (100%) rename test/build/{resolve-imports => config-alias}/packages/css-package-b/style.css (100%) rename test/build/{resolve-imports => config-alias}/public/robots.txt (100%) rename test/build/{resolve-imports => config-alias}/simple-file-extension-change-plugin.js (100%) rename test/build/{resolve-imports => config-alias}/snowpack.config.js (72%) rename test/build/{resolve-imports => config-alias}/src/components/index.js (100%) rename test/build/{resolve-imports => config-alias}/src/components/style.css (100%) rename test/build/{resolve-imports => config-alias}/src/index.html (100%) rename test/build/{resolve-imports => config-alias}/src/index.js (100%) rename test/build/{resolve-imports => config-alias}/src/sort.js (100%) rename test/build/{resolve-imports => config-alias}/src/test-mjs.mjs (100%) create mode 100644 test/build/config-meta-dir/config-meta-dir.test.js rename test/build/{meta-dir => config-meta-dir}/package.json (63%) rename test/build/{meta-dir-local-baseurl => config-meta-dir}/public/index.js (100%) rename test/build/{meta-dir-local-baseurl => config-meta-dir}/public/sub/index.js (100%) create mode 100644 test/build/config-meta-dir/snowpack.config.js delete mode 100644 test/build/config-ts-file/__snapshots__ create mode 100644 test/build/config-ts-file/config-ts-file.test.js delete mode 100644 test/build/config-ts-format/__snapshots__ delete mode 100644 test/build/config-ts-format/export.ts delete mode 100644 test/build/config-ts-format/package.json delete mode 100644 test/build/config-ts-format/snowpack.config.ts delete mode 100644 test/build/config-ts-format/src/index.js delete mode 100644 test/build/css-modules/__snapshots__ create mode 100644 test/build/css-modules/css-modules.test.js delete mode 100644 test/build/custom-modules-dir/web_modules/array-flatten.js delete mode 100644 test/build/custom-modules-dir/web_modules/import-map.json create mode 100644 test/build/import-assets/import-assets.test.js rename test/build/{resource-proxy => import-assets}/package.json (86%) create mode 100644 test/build/import-assets/snowpack.config.json rename test/build/{resource-proxy => import-assets}/src/index.js (100%) rename test/build/{plugin-optimize => import-assets}/src/logo.png (100%) rename test/build/{plugin-optimize => import-assets}/src/styles.css (100%) delete mode 100644 test/build/legacy-mount-scripts/__snapshots__ create mode 100644 test/build/legacy-mount-scripts/legacy-mount.test.js create mode 100644 test/build/legacy-mount-scripts/snowpack.config.js delete mode 100644 test/build/legacy-mount-scripts/web_modules/array-flatten.js delete mode 100644 test/build/legacy-mount-scripts/web_modules/import-map.json delete mode 100644 test/build/meta-dir-local-baseurl/__snapshots__ delete mode 100644 test/build/meta-dir-local-baseurl/package.json delete mode 100644 test/build/meta-dir-remote-baseurl/__snapshots__ delete mode 100644 test/build/meta-dir-remote-baseurl/package.json delete mode 100644 test/build/meta-dir-remote-baseurl/public/index.js delete mode 100644 test/build/meta-dir-remote-baseurl/public/sub/index.js delete mode 100644 test/build/meta-dir/__snapshots__ delete mode 100644 test/build/meta-dir/public/index.js delete mode 100644 test/build/meta-dir/public/sub/index.js delete mode 100644 test/build/module-resolution/__snapshots__ create mode 100644 test/build/module-resolution/module-resolution.test.js delete mode 100644 test/build/plugin-build-script/__snapshots__ create mode 100644 test/build/plugin-build-script/plugin-build-script.test.js rename test/build/{plugin-optimize => plugin-hook-optimize}/custom-optimize-plugin.js (100%) rename test/build/{plugin-optimize => plugin-hook-optimize}/package.json (86%) create mode 100644 test/build/plugin-hook-optimize/plugin-hook-optimize.test.js rename test/build/{plugin-optimize => plugin-hook-optimize}/snowpack.config.json (100%) rename test/build/{plugin-optimize => plugin-hook-optimize}/src/icon.svg (100%) rename test/build/{plugin-optimize => plugin-hook-optimize}/src/index.js (100%) rename test/build/{resource-proxy => plugin-hook-optimize}/src/logo.png (100%) rename test/build/{resource-proxy => plugin-hook-optimize}/src/styles.css (100%) rename test/build/{transform-sourcemap => plugin-hook-transform}/custom-transform-plugin.js (100%) rename test/build/{transform-sourcemap => plugin-hook-transform}/package.json (86%) create mode 100644 test/build/plugin-hook-transform/plugin-hook-transform.test.js rename test/build/{transform-sourcemap => plugin-hook-transform}/snowpack.config.json (88%) rename test/build/{transform-sourcemap => plugin-hook-transform}/src/index.js (100%) rename test/build/{transform-sourcemap => plugin-hook-transform}/src/submodule.ts (100%) delete mode 100644 test/build/plugin-optimize/.gitignore delete mode 100644 test/build/plugin-optimize/__snapshots__ delete mode 100644 test/build/plugin-run-script/.gitignore delete mode 100644 test/build/plugin-run-script/__snapshots__ create mode 100644 test/build/plugin-run-script/plugin-run-script.test.js create mode 100644 test/build/plugin-run-script/public/css/index.css delete mode 100644 test/build/resolve-imports/__snapshots__ delete mode 100644 test/build/resource-proxy/__snapshots__ delete mode 100644 test/build/resource-proxy/snowpack.config.json delete mode 100644 test/build/transform-sourcemap/__snapshots__ delete mode 100644 test/setup.js create mode 100644 test/test-utils.js diff --git a/.gitignore b/.gitignore index f60668fe45..78b797fbb1 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ node_modules package-lock.json create-snowpack-app/*/build test/build/**/build +test/build/**/web_modules test/create-snowpack-app/test-install test/esinstall/**/web_modules yarn-error.log diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 74cc8e8d2d..573bd30180 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -58,6 +58,8 @@ yarn test:dev # might fail on windows, see #1171 ### Snapshot tests +_Update Dec 2020: we’re working on improving this! Snapshots are now mostly gone from `test/build`, and we’ll be working through `test/esinstall` next. We‘ll wait to finish the work before updating this section, but know that this may become outdated soon._ + The way our snapshot tests work is they test Snowpack by building the codebases in `test/build`. You'll almost always have a "failed" snapshot test when you make a contribution because your new change will make the final build different. You'll want to take a new snapshot. To do this run: ```bash diff --git a/package.json b/package.json index 5d6b79b835..a0c7b4db1d 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ "lint": "lerna run lint --parallel --scope=esinstall --scope=snowpack --scope=skypack", "publish": "npm run build && lerna publish --no-private", "format": "prettier --write '{snowpack,esinstall}/src/**/*.{ts,js}' '{test,plugins}/**/*.{ts,js}' '*.{js,json,md}' '**/*.{json,md}' '.github/**/*.{md,yml}' '!**/{_dist_,build,packages,pkg,TEST_BUILD_OUT,web_modules}/**' !test/create-snowpack-app/test-install", - "pretest": "node test/setup.js", "test": "jest --testPathIgnorePatterns=/test-dev/ --test-timeout=30000", "test:dev": "jest /test-dev/ --test-timeout=30000", "test:docs": "cd www && yarn && yarn test --passWithNoTests" diff --git a/test/build/base-url-homepage/base-url-homepage.test.js b/test/build/base-url-homepage/base-url-homepage.test.js index c6f8a685b8..348a53de6f 100644 --- a/test/build/base-url-homepage/base-url-homepage.test.js +++ b/test/build/base-url-homepage/base-url-homepage.test.js @@ -1,18 +1,26 @@ -const fs = require('fs'); const path = require('path'); const cheerio = require('cheerio'); +const {setupBuildTest, readFiles} = require('../../test-utils'); -const html = fs.readFileSync(path.join(__dirname, 'build', 'index.html'), 'utf8'); +const cwd = path.join(__dirname, 'build'); -const $ = cheerio.load(html); +let files = {}; describe('packageManifest.homepage', () => { + beforeAll(() => { + setupBuildTest(__dirname); + + files = readFiles(['index.html'], {cwd}); + }); + it('baseUrl works for ', () => { + const $ = cheerio.load(files['/index.html']); expect($('link[rel="icon"]').attr('href').startsWith('/static/')).toBe(true); expect($('link[rel="stylesheet"]').attr('href').startsWith('/static/')).toBe(true); }); it('baseUrl works for - - -" -`; - -exports[`snowpack build legacy-mount-scripts: build/web_modules/array-flatten.js 1`] = ` -"/** - * Flatten an array indefinitely. - */ -function flatten(array) { - var result = []; - $flatten(array, result); - return result; -} -/** - * Internal flatten function recursively passes \`result\`. - */ -function $flatten(array, result) { - for (var i = 0; i < array.length; i++) { - var value = array[i]; - if (Array.isArray(value)) { - $flatten(value, result); - } - else { - result.push(value); - } - } -} -export { flatten };" -`; - -exports[`snowpack build legacy-mount-scripts: build/web_modules/import-map.json 1`] = ` -"{ - \\"imports\\": { - \\"array-flatten\\": \\"./array-flatten.js\\" - } -}" -`; diff --git a/test/build/legacy-mount-scripts/legacy-mount.test.js b/test/build/legacy-mount-scripts/legacy-mount.test.js new file mode 100644 index 0000000000..7742ce69a3 --- /dev/null +++ b/test/build/legacy-mount-scripts/legacy-mount.test.js @@ -0,0 +1,23 @@ +const fs = require('fs'); +const path = require('path'); +const {setupBuildTest} = require('../../test-utils'); + +const cwd = path.join(__dirname, 'build'); + +describe('config: mount scripts (legacy)', () => { + beforeAll(() => { + setupBuildTest(__dirname); + }); + + it('mounted ./src', () => { + const js = path.join(cwd, '_dist_', 'index.js'); + expect(fs.existsSync(js)).toBe(true); // file exists + expect(fs.readFileSync(js, 'utf-8')).toBeTruthy(); // file has content + }); + + it('mounted ./public', () => { + const html = path.join(cwd, 'index.html'); + expect(fs.existsSync(html)).toBe(true); // file exists + expect(fs.readFileSync(html, 'utf-8')).toBeTruthy(); // file has content + }); +}); diff --git a/test/build/legacy-mount-scripts/package.json b/test/build/legacy-mount-scripts/package.json index 2e8c506fca..3d4af91d55 100644 --- a/test/build/legacy-mount-scripts/package.json +++ b/test/build/legacy-mount-scripts/package.json @@ -7,16 +7,6 @@ "start": "snowpack dev", "testbuild": "snowpack build" }, - "snowpack": { - "mount": { - "./public": "/", - "./src": "/_dist_" - }, - "buildOptions": { - "baseUrl": "/static/", - "minify": false - } - }, "dependencies": { "array-flatten": "^3.0.0" }, diff --git a/test/build/legacy-mount-scripts/snowpack.config.js b/test/build/legacy-mount-scripts/snowpack.config.js new file mode 100644 index 0000000000..524df65049 --- /dev/null +++ b/test/build/legacy-mount-scripts/snowpack.config.js @@ -0,0 +1,6 @@ +module.exports = { + scripts: { + 'mount:src': 'mount src --to /_dist_', + 'mount:public': 'mount public --to /', + }, +}; diff --git a/test/build/legacy-mount-scripts/web_modules/array-flatten.js b/test/build/legacy-mount-scripts/web_modules/array-flatten.js deleted file mode 100644 index 1cc2d53af8..0000000000 --- a/test/build/legacy-mount-scripts/web_modules/array-flatten.js +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Flatten an array indefinitely. - */ -function flatten(array) { - var result = []; - $flatten(array, result); - return result; -} -/** - * Internal flatten function recursively passes `result`. - */ -function $flatten(array, result) { - for (var i = 0; i < array.length; i++) { - var value = array[i]; - if (Array.isArray(value)) { - $flatten(value, result); - } - else { - result.push(value); - } - } -} - -export { flatten }; diff --git a/test/build/legacy-mount-scripts/web_modules/import-map.json b/test/build/legacy-mount-scripts/web_modules/import-map.json deleted file mode 100644 index 704b8ba214..0000000000 --- a/test/build/legacy-mount-scripts/web_modules/import-map.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "imports": { - "array-flatten": "./array-flatten.js" - } -} \ No newline at end of file diff --git a/test/build/meta-dir-local-baseurl/__snapshots__ b/test/build/meta-dir-local-baseurl/__snapshots__ deleted file mode 100644 index 7f4b65157a..0000000000 --- a/test/build/meta-dir-local-baseurl/__snapshots__ +++ /dev/null @@ -1,33 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`snowpack build meta-dir-local-baseurl: allFiles 1`] = ` -Array [ - "index.js", - "static/snowpack/env.js", - "sub/index.js", -] -`; - -exports[`snowpack build meta-dir-local-baseurl: build/index.js 1`] = ` -"import __SNOWPACK_ENV__ from './static/snowpack/env.js'; -import.meta.env = __SNOWPACK_ENV__; -// public/index.js - Test import URLs from the \\"/\\" directory -export default function doNothing() { - // I do nothing 🎉 -} -// Triggers a snowpack meta import URL -console.log(import.meta.env)" -`; - -exports[`snowpack build meta-dir-local-baseurl: build/static/snowpack/env.js 1`] = `"export default {\\"MODE\\":\\"production\\",\\"NODE_ENV\\":\\"production\\",\\"SSR\\":false};"`; - -exports[`snowpack build meta-dir-local-baseurl: build/sub/index.js 1`] = ` -"import __SNOWPACK_ENV__ from '../static/snowpack/env.js'; -import.meta.env = __SNOWPACK_ENV__; -// public/sub/index.js - Test import URLs from the \\"/sub/\\" directory -export default function doNothing() { - // I do nothing 🎉 -} -// Triggers a snowpack meta import URL -console.log(import.meta.env)" -`; diff --git a/test/build/meta-dir-local-baseurl/package.json b/test/build/meta-dir-local-baseurl/package.json deleted file mode 100644 index f4fed44b9f..0000000000 --- a/test/build/meta-dir-local-baseurl/package.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "private": true, - "version": "1.0.1", - "name": "@snowpack/test-meta-dir-local-baseurl", - "description": "Test that makes sure that the metaDir can be configured and still build", - "scripts": { - "prepare": "snowpack", - "testbuild": "snowpack build" - }, - "snowpack": { - "mount": { - "./public": "/" - }, - "buildOptions": { - "baseUrl": "/builds", - "metaDir": "/static/snowpack", - "minify": false - } - }, - "dependencies": { - "shallow-equal": "^1.2.1" - }, - "devDependencies": { - "snowpack": "^2.14.3" - } -} diff --git a/test/build/meta-dir-remote-baseurl/__snapshots__ b/test/build/meta-dir-remote-baseurl/__snapshots__ deleted file mode 100644 index 76b08fc1e3..0000000000 --- a/test/build/meta-dir-remote-baseurl/__snapshots__ +++ /dev/null @@ -1,33 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`snowpack build meta-dir-remote-baseurl: allFiles 1`] = ` -Array [ - "index.js", - "static/snowpack/env.js", - "sub/index.js", -] -`; - -exports[`snowpack build meta-dir-remote-baseurl: build/index.js 1`] = ` -"import __SNOWPACK_ENV__ from './static/snowpack/env.js'; -import.meta.env = __SNOWPACK_ENV__; -// public/index.js - Test import URLs from the \\"/\\" directory -export default function doNothing() { - // I do nothing 🎉 -} -// Triggers a snowpack meta import URL -console.log(import.meta.env)" -`; - -exports[`snowpack build meta-dir-remote-baseurl: build/static/snowpack/env.js 1`] = `"export default {\\"MODE\\":\\"production\\",\\"NODE_ENV\\":\\"production\\",\\"SSR\\":false};"`; - -exports[`snowpack build meta-dir-remote-baseurl: build/sub/index.js 1`] = ` -"import __SNOWPACK_ENV__ from '../static/snowpack/env.js'; -import.meta.env = __SNOWPACK_ENV__; -// public/sub/index.js - Test import URLs from the \\"/sub/\\" directory -export default function doNothing() { - // I do nothing 🎉 -} -// Triggers a snowpack meta import URL -console.log(import.meta.env)" -`; diff --git a/test/build/meta-dir-remote-baseurl/package.json b/test/build/meta-dir-remote-baseurl/package.json deleted file mode 100644 index b1741231ed..0000000000 --- a/test/build/meta-dir-remote-baseurl/package.json +++ /dev/null @@ -1,26 +0,0 @@ -{ - "private": true, - "version": "1.0.1", - "name": "@snowpack/test-meta-dir-remote-baseurl", - "description": "Test that makes sure that the metaDir can be configured and still build", - "scripts": { - "prepare": "snowpack", - "testbuild": "snowpack build" - }, - "snowpack": { - "mount": { - "./public": "/" - }, - "buildOptions": { - "baseUrl": "https://www.cdn.com/sub_path/", - "metaDir": "/static/snowpack", - "minify": false - } - }, - "dependencies": { - "shallow-equal": "^1.2.1" - }, - "devDependencies": { - "snowpack": "^2.14.3" - } -} diff --git a/test/build/meta-dir-remote-baseurl/public/index.js b/test/build/meta-dir-remote-baseurl/public/index.js deleted file mode 100644 index 97dc4a22cb..0000000000 --- a/test/build/meta-dir-remote-baseurl/public/index.js +++ /dev/null @@ -1,7 +0,0 @@ -// public/index.js - Test import URLs from the "/" directory -export default function doNothing() { - // I do nothing 🎉 -} - -// Triggers a snowpack meta import URL -console.log(import.meta.env) \ No newline at end of file diff --git a/test/build/meta-dir-remote-baseurl/public/sub/index.js b/test/build/meta-dir-remote-baseurl/public/sub/index.js deleted file mode 100644 index be5244e079..0000000000 --- a/test/build/meta-dir-remote-baseurl/public/sub/index.js +++ /dev/null @@ -1,7 +0,0 @@ -// public/sub/index.js - Test import URLs from the "/sub/" directory -export default function doNothing() { - // I do nothing 🎉 -} - -// Triggers a snowpack meta import URL -console.log(import.meta.env) \ No newline at end of file diff --git a/test/build/meta-dir/__snapshots__ b/test/build/meta-dir/__snapshots__ deleted file mode 100644 index c5c78eaa98..0000000000 --- a/test/build/meta-dir/__snapshots__ +++ /dev/null @@ -1,33 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`snowpack build meta-dir: allFiles 1`] = ` -Array [ - "index.js", - "static/snowpack/env.js", - "sub/index.js", -] -`; - -exports[`snowpack build meta-dir: build/index.js 1`] = ` -"import __SNOWPACK_ENV__ from './static/snowpack/env.js'; -import.meta.env = __SNOWPACK_ENV__; -// public/index.js - Test import URLs from the \\"/\\" directory -export default function doNothing() { - // I do nothing 🎉 -} -// Triggers a snowpack meta import URL -console.log(import.meta.env)" -`; - -exports[`snowpack build meta-dir: build/static/snowpack/env.js 1`] = `"export default {\\"MODE\\":\\"production\\",\\"NODE_ENV\\":\\"production\\",\\"SSR\\":false};"`; - -exports[`snowpack build meta-dir: build/sub/index.js 1`] = ` -"import __SNOWPACK_ENV__ from '../static/snowpack/env.js'; -import.meta.env = __SNOWPACK_ENV__; -// public/sub/index.js - Test import URLs from the \\"/sub/\\" directory -export default function doNothing() { - // I do nothing 🎉 -} -// Triggers a snowpack meta import URL -console.log(import.meta.env)" -`; diff --git a/test/build/meta-dir/public/index.js b/test/build/meta-dir/public/index.js deleted file mode 100644 index 97dc4a22cb..0000000000 --- a/test/build/meta-dir/public/index.js +++ /dev/null @@ -1,7 +0,0 @@ -// public/index.js - Test import URLs from the "/" directory -export default function doNothing() { - // I do nothing 🎉 -} - -// Triggers a snowpack meta import URL -console.log(import.meta.env) \ No newline at end of file diff --git a/test/build/meta-dir/public/sub/index.js b/test/build/meta-dir/public/sub/index.js deleted file mode 100644 index be5244e079..0000000000 --- a/test/build/meta-dir/public/sub/index.js +++ /dev/null @@ -1,7 +0,0 @@ -// public/sub/index.js - Test import URLs from the "/sub/" directory -export default function doNothing() { - // I do nothing 🎉 -} - -// Triggers a snowpack meta import URL -console.log(import.meta.env) \ No newline at end of file diff --git a/test/build/module-resolution/__snapshots__ b/test/build/module-resolution/__snapshots__ deleted file mode 100644 index c5e924d5e9..0000000000 --- a/test/build/module-resolution/__snapshots__ +++ /dev/null @@ -1,66 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`snowpack build module-resolution: allFiles 1`] = ` -Array [ - "__snowpack__/env.js", - "folder-1/folder-2/index.html", - "folder-1/index.html", - "index.html", - "src.js", - "web_modules/import-map.json", - "web_modules/preact.js", -] -`; - -exports[`snowpack build module-resolution: build/__snowpack__/env.js 1`] = `"export default {\\"MODE\\":\\"production\\",\\"NODE_ENV\\":\\"production\\",\\"SSR\\":false};"`; - -exports[`snowpack build module-resolution: build/folder-1/folder-2/index.html 1`] = ` -" - - - -" -`; - -exports[`snowpack build module-resolution: build/folder-1/index.html 1`] = ` -" - - - -" -`; - -exports[`snowpack build module-resolution: build/index.html 1`] = ` -" - - - -" -`; - -exports[`snowpack build module-resolution: build/src.js 1`] = ` -"import './web_modules/preact.js'; -console.log('I’m JS');" -`; - -exports[`snowpack build module-resolution: build/web_modules/import-map.json 1`] = ` -"{ - \\"imports\\": { - \\"preact\\": \\"./preact.js\\" - } -}" -`; - -exports[`snowpack build module-resolution: build/web_modules/preact.js 1`] = ` -"var n,l,u,i,t,o,r,f={},e=[],c=/acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|itera/i;function s(n,l){for(var u in l)n[u]=l[u];return n}function a(n){var l=n.parentNode;l&&l.removeChild(n);}function v(n,l,u){var i,t,o,r=arguments,f={};for(o in l)\\"key\\"==o?i=l[o]:\\"ref\\"==o?t=l[o]:f[o]=l[o];if(arguments.length>3)for(u=[u],o=3;o1&&T(t,l,u),l=x(u,t,t,n.__k,null,t.__e,l),\\"function\\"==typeof n.type&&(n.__d=l)));}function $(l,u,i,t,o,r,f,e,c){var a,v,h,y,_,w,k,g,b,x,A,P=u.type;if(void 0!==u.constructor)return null;null!=i.__h&&(c=i.__h,e=u.__e=i.__e,u.__h=null,r=[e]),(a=n.__b)&&a(u);try{n:if(\\"function\\"==typeof P){if(g=u.props,b=(a=P.contextType)&&t[a.__c],x=a?b?b.props.value:a.__:t,i.__c?k=(v=u.__c=i.__c).__=v.__E:(\\"prototype\\"in P&&P.prototype.render?u.__c=v=new P(g,x):(u.__c=v=new d(g,x),v.constructor=P,v.render=M),b&&b.sub(v),v.props=g,v.state||(v.state={}),v.context=x,v.__n=t,h=v.__d=!0,v.__h=[]),null==v.__s&&(v.__s=v.state),null!=P.getDerivedStateFromProps&&(v.__s==v.state&&(v.__s=s({},v.__s)),s(v.__s,P.getDerivedStateFromProps(g,v.__s))),y=v.props,_=v.state,h)null==P.getDerivedStateFromProps&&null!=v.componentWillMount&&v.componentWillMount(),null!=v.componentDidMount&&v.__h.push(v.componentDidMount);else {if(null==P.getDerivedStateFromProps&&g!==y&&null!=v.componentWillReceiveProps&&v.componentWillReceiveProps(g,x),!v.__e&&null!=v.shouldComponentUpdate&&!1===v.shouldComponentUpdate(g,v.__s,x)||u.__v===i.__v){v.props=g,v.state=v.__s,u.__v!==i.__v&&(v.__d=!1),v.__v=u,u.__e=i.__e,u.__k=i.__k,v.__h.length&&f.push(v),T(u,e,l);break n}null!=v.componentWillUpdate&&v.componentWillUpdate(g,v.__s,x),null!=v.componentDidUpdate&&v.__h.push(function(){v.componentDidUpdate(y,_,w);});}v.context=x,v.props=g,v.state=v.__s,(a=n.__r)&&a(u),v.__d=!1,v.__v=u,v.__P=l,a=v.render(v.props,v.state,v.context),v.state=v.__s,null!=v.getChildContext&&(t=s(s({},t),v.getChildContext())),h||null==v.getSnapshotBeforeUpdate||(w=v.getSnapshotBeforeUpdate(y,_)),A=null!=a&&a.type==p&&null==a.key?a.props.children:a,m(l,Array.isArray(A)?A:[A],u,i,t,o,r,f,e,c),v.base=u.__e,u.__h=null,v.__h.length&&f.push(v),k&&(v.__E=v.__=null),v.__e=!1;}else null==r&&u.__v===i.__v?(u.__k=i.__k,u.__e=i.__e):u.__e=H(i.__e,u,i,t,o,r,f,c);(a=n.diffed)&&a(u);}catch(l){u.__v=null,(c||null!=r)&&(u.__e=e,u.__h=!!c,r[r.indexOf(e)]=null),n.__e(l,u,i);}return u.__e}function j(l,u){n.__c&&n.__c(u,l),l.some(function(u){try{l=u.__h,u.__h=[],l.some(function(n){n.call(u);});}catch(l){n.__e(l,u.__v);}});}function H(n,l,u,i,t,o,r,c){var s,a,v,h,y,p=u.props,d=l.props;if(t=\\"svg\\"===l.type||t,null!=o)for(s=0;s3)for(u=[u],o=3;o { + beforeAll(() => { + setupBuildTest(__dirname); + + files = readFiles( + ['src.js', 'index.html', 'folder-1/index.html', 'folder-1/folder-2/index.html'], + {cwd}, + ); + }); + + it('JS: resolves web_modules relatively', () => { + expect(files['/src.js']).toEqual(expect.stringContaining(`import './web_modules/preact.js';`)); + }); + + it('HTML: - - - - - - -
import React from 'react';
- - -" -`; - -exports[`snowpack build resolve-imports: build/_dist_/index.js 1`] = ` -"// Path aliases -import {flatten} from '../TEST_WMU/array-flatten.js'; -import * as aliasedDep from '../TEST_WMU/array-flatten.js'; -console.log(flatten, aliasedDep); -// Importing a file -import sort from './sort.js'; // relative import -import sort_ from './sort.js'; // absolute import -import sort__ from './sort.js'; // bare import using alias -import sort___ from './sort.js'; // bare import using alias + extension -import sort____ from './sort.js'; // bare import using alias with trailing slash -import sort_____ from './sort.js'; // bare import using file alias -console.log(sort, sort_, sort__, sort___, sort___, sort____, sort_____); -// Note: file does not need to exist for these checks: -import svelteFile from './foo.js'; // plugin-provided file extension -import svelteFile_ from './foo.js'; // plugin-provided, missing file extension -import svelteFile__ from '../foo.js'; // absolute URL, plugin-provided file extension -import svelteFile___ from '../foo.js'; // absolute URL, missing file extension -console.log(svelteFile, svelteFile_, svelteFile__, svelteFile___); -// Importing a directory index.js file -import components from './components/index.js'; // relative import -import components______ from './components/index.js'; // relative import with trailing slash -import components_ from './components/index.js'; // relative import with index appended -import components__ from './components/index.js'; // relative import with index appended -import components___ from './components/index.js'; // bare import using alias -import components____ from './components/index.js'; // bare import using alias and index appended -import components_____ from './components/index.js'; // bare import using alias and index.js appended -import components2 from './components/index.js'; // alias % to '.' -console.log( - components, - components_, - components__, - components___, - components____, - components_____, - components______, - components2, -); -// Importing something that isn't JS -import styles from './components/style.css.proxy.js'; // relative import -import styles_ from './components/style.css.proxy.js'; // relative import -console.log(styles, styles_); -import adSvg from '../TEST_WMU/@fortawesome/fontawesome-free/svgs/solid/ad.svg.proxy.js'; -console.log(adSvg); -// Importing across mounted directories -import robotsTxtRef from '../robots.txt.proxy.js'; -import robotsTxtRef_ from '../robots.txt.proxy.js'; -console.log(robotsTxtRef, robotsTxtRef_);" -`; - -exports[`snowpack build resolve-imports: build/_dist_/sort.js 1`] = `"export default (arr) => arr.sort();"`; - -exports[`snowpack build resolve-imports: build/_dist_/test-mjs.js 1`] = ` -"import {flatten} from \\"../TEST_WMU/array-flatten.js\\"; -console.log(flatten);" -`; - -exports[`snowpack build resolve-imports: build/TEST_WMU/@css/package-b/style.css 1`] = ` -"body { - color: blue; -}" -`; - -exports[`snowpack build resolve-imports: build/TEST_WMU/@css/package-b/style.css.proxy.js 1`] = ` -"// [snowpack] add styles to the page (skip if no document exists) -if (typeof document !== 'undefined') { - const code = \\"body { color: blue;}\\"; - const styleEl = document.createElement(\\"style\\"); - const codeEl = document.createTextNode(code); - styleEl.type = 'text/css'; - styleEl.appendChild(codeEl); - document.head.appendChild(styleEl); -}" -`; - -exports[`snowpack build resolve-imports: build/TEST_WMU/@fortawesome/fontawesome-free/svgs/solid/ad.svg.proxy.js 1`] = `"export default \\"/TEST_WMU/@fortawesome/fontawesome-free/svgs/solid/ad.svg\\";"`; - -exports[`snowpack build resolve-imports: build/TEST_WMU/array-flatten.js 1`] = ` -"/** - * Flatten an array indefinitely. - */ -function flatten(array) { - var result = []; - $flatten(array, result); - return result; -} -/** - * Internal flatten function recursively passes \`result\`. - */ -function $flatten(array, result) { - for (var i = 0; i < array.length; i++) { - var value = array[i]; - if (Array.isArray(value)) { - $flatten(value, result); - } - else { - result.push(value); - } - } -} -export { flatten };" -`; - -exports[`snowpack build resolve-imports: build/TEST_WMU/css-package-a/style.css 1`] = ` -"body { - color: red; -}" -`; - -exports[`snowpack build resolve-imports: build/TEST_WMU/css-package-a/style.css.proxy.js 1`] = ` -"// [snowpack] add styles to the page (skip if no document exists) -if (typeof document !== 'undefined') { - const code = \\"body { color: red;}\\"; - const styleEl = document.createElement(\\"style\\"); - const codeEl = document.createTextNode(code); - styleEl.type = 'text/css'; - styleEl.appendChild(codeEl); - document.head.appendChild(styleEl); -}" -`; - -exports[`snowpack build resolve-imports: build/TEST_WMU/import-map.json 1`] = ` -"{ - \\"imports\\": { - \\"@css/package-b/style.css\\": \\"./@css/package-b/style.css\\", - \\"@fortawesome/fontawesome-free/svgs/solid/ad.svg\\": \\"./@fortawesome/fontawesome-free/svgs/solid/ad.svg\\", - \\"aliased-dep\\": \\"./array-flatten.js\\", - \\"array-flatten\\": \\"./array-flatten.js\\", - \\"css-package-a/style.css\\": \\"./css-package-a/style.css\\" - } -}" -`; - -exports[`snowpack build resolve-imports: build/robots.txt.proxy.js 1`] = `"export default \\"/robots.txt\\";"`; diff --git a/test/build/resource-proxy/__snapshots__ b/test/build/resource-proxy/__snapshots__ deleted file mode 100644 index 0dadb9e559..0000000000 --- a/test/build/resource-proxy/__snapshots__ +++ /dev/null @@ -1,40 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`snowpack build resource-proxy: allFiles 1`] = ` -Array [ - "__snowpack__/env.js", - "_dist_/index.js", - "_dist_/logo.png", - "_dist_/logo.png.proxy.js", - "_dist_/styles.css", - "_dist_/styles.css.proxy.js", -] -`; - -exports[`snowpack build resource-proxy: build/__snowpack__/env.js 1`] = `"export default {\\"MODE\\":\\"production\\",\\"NODE_ENV\\":\\"production\\",\\"SSR\\":false};"`; - -exports[`snowpack build resource-proxy: build/_dist_/index.js 1`] = ` -"import './styles.css.proxy.js'; -import './logo.png.proxy.js'; -console.log('loaded');" -`; - -exports[`snowpack build resource-proxy: build/_dist_/logo.png.proxy.js 1`] = `"export default \\"/_dist_/logo.png\\";"`; - -exports[`snowpack build resource-proxy: build/_dist_/styles.css 1`] = ` -"body { - font-family: fantasy; -}" -`; - -exports[`snowpack build resource-proxy: build/_dist_/styles.css.proxy.js 1`] = ` -"// [snowpack] add styles to the page (skip if no document exists) -if (typeof document !== 'undefined') { - const code = \\"body { font-family: fantasy;}\\"; - const styleEl = document.createElement(\\"style\\"); - const codeEl = document.createTextNode(code); - styleEl.type = 'text/css'; - styleEl.appendChild(codeEl); - document.head.appendChild(styleEl); -}" -`; diff --git a/test/build/resource-proxy/snowpack.config.json b/test/build/resource-proxy/snowpack.config.json deleted file mode 100644 index 0f968f42e1..0000000000 --- a/test/build/resource-proxy/snowpack.config.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "mount": { - "./src": "/_dist_" - }, - "buildOptions": { - "minify": false - } -} diff --git a/test/build/transform-sourcemap/__snapshots__ b/test/build/transform-sourcemap/__snapshots__ deleted file mode 100644 index 2d23b91c12..0000000000 --- a/test/build/transform-sourcemap/__snapshots__ +++ /dev/null @@ -1,30 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`snowpack build transform-sourcemap: allFiles 1`] = ` -Array [ - "__snowpack__/env.js", - "_dist_/index.js", - "_dist_/index.js.map", - "_dist_/submodule.js", - "_dist_/submodule.js.map", -] -`; - -exports[`snowpack build transform-sourcemap: build/__snowpack__/env.js 1`] = `"export default {\\"MODE\\":\\"production\\",\\"NODE_ENV\\":\\"production\\",\\"SSR\\":false};"`; - -exports[`snowpack build transform-sourcemap: build/_dist_/index.js 1`] = ` -"import './submodule.js'; -console.log('transformed'); -console.log('loaded'); -//# sourceMappingURL=index.js.map" -`; - -exports[`snowpack build transform-sourcemap: build/_dist_/index.js.map 1`] = `"{\\"version\\":3,\\"file\\":null,\\"sources\\":[/HOME/snowpack/test/build/transform-sourcemap/src/index.js],\\"sourcesContent\\":[\\"import './submodule.ts';console.log('loaded');\\"],\\"names\\":[],\\"mappings\\":\\"AAAA;AACA;;AACA;\\"}"`; - -exports[`snowpack build transform-sourcemap: build/_dist_/submodule.js 1`] = ` -"console.log('transformed'); -console.log(\\"ts loaded\\"); -//# sourceMappingURL=submodule.js.map" -`; - -exports[`snowpack build transform-sourcemap: build/_dist_/submodule.js.map 1`] = `"{\\"version\\":3,\\"sources\\":[/HOME/snowpack/test/build/transform-sourcemap/src/submodule.ts],\\"names\\":[],\\"mappings\\":\\";AAAA\\",\\"sourcesContent\\":[\\"console.log(\\\\\\"ts loaded\\\\\\");\\"]}"`; diff --git a/test/create-snowpack-app/create-snowpack-app.test.js b/test/create-snowpack-app/create-snowpack-app.test.js index d7a53677e9..a1e84cbe37 100644 --- a/test/create-snowpack-app/create-snowpack-app.test.js +++ b/test/create-snowpack-app/create-snowpack-app.test.js @@ -3,7 +3,6 @@ const path = require('path'); const execa = require('execa'); const rimraf = require('rimraf'); const glob = require('glob'); -const strpAnsi = require('strip-ansi'); const TEMPLATES_DIR = path.resolve(__dirname, '..', '..', 'create-snowpack-app'); const templates = fs.readdirSync(TEMPLATES_DIR).filter((dir) => dir.startsWith('app-template-')); diff --git a/test/setup.js b/test/setup.js deleted file mode 100644 index cdd982f291..0000000000 --- a/test/setup.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * This contains no tests; it only sets up the directory. - */ -const fs = require('fs'); -const glob = require('glob'); -const path = require('path'); -const execa = require('execa'); - -// setup for /tests/build/* -async function setupBuildTests() { - // set NODE_ENV = 'test' - const originalEnv = process.env.NODE_ENV; - process.env.NODE_ENV = 'test'; - - console.time(`Building tests…`); - await Promise.all( - glob - .sync(path.join(__dirname, 'build', '*/package.json')) - .map(path.dirname) - .map((testdir) => { - const capitalize = testdir === 'entrypoint-ids' && os.platform() === 'win32'; // for Windows, we capitalize this one directory to see if Snowpack can still resolve - return execa('yarn', ['testbuild'], {cwd: capitalize ? testdir.toUpperCase() : testdir}); - }), - ); - console.timeEnd(`Building tests…`); - - // reset NODE_ENV - process.env.NODE_ENV = originalEnv; -} -setupBuildTests(); diff --git a/test/test-utils.js b/test/test-utils.js new file mode 100644 index 0000000000..cc14ed6fc2 --- /dev/null +++ b/test/test-utils.js @@ -0,0 +1,23 @@ +const fs = require('fs'); +const path = require('path'); +const {execSync} = require('child_process'); + +/** setup for /tests/build/* */ +function setupBuildTest(cwd) { + execSync('yarn testbuild', {cwd}); +} +exports.setupBuildTest = setupBuildTest; + +/** take an array of files, return contents of files (won’t read entire directory for performance) */ +function readFiles(files, {cwd}) { + if (!cwd) throw new Error(`cwd option missing, ex: readFiles(files, { cwd: __dirname })`); + + const contents = {}; + (Array.isArray(files) ? files : [files]).forEach((f) => { + const relativePath = f.replace(/^\/?/, '/'); + const filepath = path.join(cwd, ...f.split('/')); + contents[relativePath] = fs.readFileSync(filepath, 'utf-8'); + }); + return contents; +} +exports.readFiles = readFiles; diff --git a/yarn.lock b/yarn.lock index 5009b7e1e1..950f764509 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1262,7 +1262,7 @@ exec-sh "^0.3.2" minimist "^1.2.0" -"@css/package-b@file:./test/build/resolve-imports/packages/css-package-b": +"@css/package-b@file:./test/build/config-alias/packages/css-package-b": version "1.2.3" "@emotion/is-prop-valid@^0.8.5": @@ -5659,7 +5659,7 @@ css-loader@^4.3.0: "css-mock-pkg-b@file:./test/esinstall/dep-list-css/packages/css-mock-pkg-b": version "1.0.0" -"css-package-a@file:./test/build/resolve-imports/packages/css-package-a": +"css-package-a@file:./test/build/config-alias/packages/css-package-a": version "1.2.3" css-select-base-adapter@^0.1.1: