Skip to content

Commit

Permalink
Improve remaining build tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drwpow committed Dec 5, 2020
1 parent 9bd54e7 commit a0b6483
Show file tree
Hide file tree
Showing 109 changed files with 640 additions and 1,182 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 11 additions & 3 deletions test/build/base-url-homepage/base-url-homepage.test.js
Original file line number Diff line number Diff line change
@@ -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 <link>', () => {
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 <script>', () => {
const $ = cheerio.load(files['/index.html']);
expect($('script').attr('src').startsWith('/static/')).toBe(true);
});
});
14 changes: 11 additions & 3 deletions test/build/base-url-remote/base-url-remote.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
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 = {};
let $;

describe('buildOptions.baseUrl', () => {
beforeAll(() => {
setupBuildTest(__dirname);

files = readFiles(['index.html'], {cwd});
$ = cheerio.load(files['/index.html']);
});

it('baseUrl works for <link>', () => {
expect($('link[rel="icon"]').attr('href').startsWith('https://www.example.com/')).toBe(true);
expect($('link[rel="stylesheet"]').attr('href').startsWith('https://www.example.com/')).toBe(
Expand Down
27 changes: 18 additions & 9 deletions test/build/base-url/base-url.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
const {setupBuildTest, readFiles} = require('../../test-utils');

const cwd = path.join(__dirname, 'build');

const html = fs.readFileSync(path.join(cwd, 'index.html'), 'utf8');
const indexJS = fs.readFileSync(path.join(cwd, 'index.js'), 'utf-8');
const distJS = fs.readFileSync(path.join(cwd, '_dist_', 'index.js'), 'utf-8');
const $ = cheerio.load(html);
let files = {};
let $;

describe('buildOptions.baseUrl', () => {
beforeAll(() => {
setupBuildTest(__dirname);

files = readFiles(['index.html', 'index.js', '_dist_/index.js'], {cwd});
$ = cheerio.load(files['/index.html']);
});

it('baseUrl works for <link>', () => {
expect($('link[rel="icon"]').attr('href').startsWith('/static/')).toBe(true);
expect($('link[rel="stylesheet"]').attr('href').startsWith('/static/')).toBe(true);
Expand All @@ -21,15 +26,19 @@ describe('buildOptions.baseUrl', () => {

it('import.meta.env works', () => {
// env is present in index.js
expect(indexJS).toEqual(
expect(files['/index.js']).toEqual(
expect.stringContaining(`import __SNOWPACK_ENV__ from './__snowpack__/env.js';`),
);
expect(indexJS).toEqual(expect.stringContaining(`import.meta.env = __SNOWPACK_ENV__;`));
expect(files['/index.js']).toEqual(
expect.stringContaining(`import.meta.env = __SNOWPACK_ENV__;`),
);

// env is present in _dist_/index.js too
expect(distJS).toEqual(
expect(files['/_dist_/index.js']).toEqual(
expect.stringContaining(`import __SNOWPACK_ENV__ from '../__snowpack__/env.js';`),
);
expect(distJS).toEqual(expect.stringContaining(`import.meta.env = __SNOWPACK_ENV__;`));
expect(files['/_dist_/index.js']).toEqual(
expect.stringContaining(`import.meta.env = __SNOWPACK_ENV__;`),
);
});
});
5 changes: 5 additions & 0 deletions test/build/bugfix-named-import/bugfix-named-import.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
const fs = require('fs');
const path = require('path');
const {setupBuildTest} = require('../../test-utils');

describe('bugfix: named import', () => {
beforeAll(() => {
setupBuildTest(__dirname);
});

// if this file built successfully, then the ipmort worked
it('built', () => {
const webModule = path.join(__dirname, 'build', 'web_modules', 'array-flatten.js');
Expand Down
113 changes: 0 additions & 113 deletions test/build/build.test.js

This file was deleted.

16 changes: 10 additions & 6 deletions test/build/cdn/cdn.test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
const fs = require('fs');
const path = require('path');
const cheerio = require('cheerio');
const {setupBuildTest, readFiles} = require('../../test-utils');

const cwd = path.join(__dirname, 'build');
let files = {};

const html = fs.readFileSync(path.join(cwd, 'index.html'), 'utf-8');
const distJS = fs.readFileSync(path.join(cwd, '_dist_', 'index.js'), 'utf-8');
describe('CDN URLs', () => {
beforeAll(() => {
setupBuildTest(__dirname);

const $ = cheerio.load(html);
files = readFiles(['index.html', '_dist_/index.js'], {cwd});
});

describe('CDN URLs', () => {
it('HTML: preserves remote URLs', () => {
const $ = cheerio.load(files['/index.html']);
expect($('script[src^="https://unpkg.com"]')).toBeTruthy();
});

it('JS: preserves CDN URLs', () => {
expect(distJS).toEqual(
expect(files['/_dist_/index.js']).toEqual(
expect.stringContaining('import React from "https://cdn.pika.dev/react@^16.13.1";'),
);
expect(distJS).toEqual(
expect(files['/_dist_/index.js']).toEqual(
expect.stringContaining('import ReactDOM from "https://cdn.pika.dev/react-dom@^16.13.1";'),
);
});
Expand Down
Loading

0 comments on commit a0b6483

Please sign in to comment.