Skip to content

Commit

Permalink
Simplify test suite (#2858)
Browse files Browse the repository at this point in the history
* first fixtures tests

* fix bad custom handler for already bad CI issue
  • Loading branch information
FredKSchott committed Apr 4, 2021
1 parent d542762 commit 18b9f09
Show file tree
Hide file tree
Showing 32 changed files with 247 additions and 321 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ lerna-debug.log
node_modules
package-lock.json
create-snowpack-app/*/build
test/__temp__
test/build/**/build
test/build/**/TEST_BUILD_OUT
test/build/**/web_modules
Expand Down
14 changes: 9 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,18 @@ From the repository's root folder, run
```bash
yarn build
yarn test
yarn test:dev # might fail on windows, see #1171
yarn test:dev # sometimes flaky 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._
_Update March 2021: we’re working on improving this and would love your help converting old test directories to smaller TypeScript tests/fixtures!_

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:
All new tests should live in either `test/snowpack`, `test/esinstall`, or the `test` directory of one of our plugins (ex: `plugins/plugin-dotenv/test/plugin.test.js`). `test/build` is considered legacy and is in the process of getting converted into `test/snowpack`.

The way our snapshot tests work is they test Snowpack by building a project. Because Snowpack's build method uses the dev server internally, this is a good way to test the build behavior of both dev & build together.

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
yarn test -u
Expand All @@ -81,10 +85,10 @@ You'll notice this changes the snapshot file. Commit this change and submit it a

### Filtering tests

You can filter the tests that are being run using Jest's [`--testNamePattern`](https://jestjs.io/docs/en/cli#--testnamepatternregex) (alias: `-t`) CLI option. You can ignore the `123 snapshots obsolete` messages.
Jest supports basic test filtering to only run a subset of our test suite. The [`--testNamePattern`](https://jestjs.io/docs/en/cli#--testnamepatternregex) flag also works as well.

```bash
yarn test --testNamePattern treeshake
yarn test treeshake
```

## Run local snowpack in another project
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ module.exports = {
'<rootDir>/test/create-snowpack-app/test-install', // don’t run tests inside our mock create-snowpack-app install
'<rootDir>/www', // docs has its own tests
],
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
globalSetup: '<rootDir>/jest.setup.js',
testEnvironment: 'node',
};
11 changes: 10 additions & 1 deletion jest.setup.js
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
process.env.NO_COLOR = true; // enable NO_COLOR mode for Jest
module.exports = async () => {
// enable NO_COLOR mode for Jest
process.env.NO_COLOR = true;
// Clear the temp directory
const path = require('path');
const rimraf = require('rimraf');
const fs = require('fs');
rimraf.sync(path.join(__dirname, 'test', '__temp__'));
fs.mkdirSync(path.join(__dirname, 'test', '__temp__'));
};
2 changes: 1 addition & 1 deletion snowpack/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ function normalizeMount(config: SnowpackConfig) {
}
// if no mounted directories, mount the root directory to the base URL
if (!Object.keys(normalizedMount).length) {
normalizedMount[process.cwd()] = {
normalizedMount[config.root] = {
url: '/',
static: false,
resolve: true,
Expand Down
11 changes: 10 additions & 1 deletion snowpack/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ export function deleteFromBuildSafe(dir: string, config: SnowpackConfig) {

/** Read file from disk; return a string if it’s a code file */
export async function readFile(filepath: string): Promise<string | Buffer> {
const data = await fs.promises.readFile(filepath);
let data = await fs.promises.readFile(filepath);
if (!data) {
console.error(
`Unexpected Node.js error: readFile(${filepath}) returned undefined.\n\n` +
`Somehow in Github CI / Jest its possible for fs.promises.readFile to return undefined.\n` +
`This should be impossible, and has not yet been reproduced in the real world, but we do see it in our own CI.\n` +
`If you are seeing this error, please report!`,
);
data = fs.readFileSync(filepath);
}
const isBinary = await isBinaryFile(data);
return isBinary ? data : data.toString('utf8');
}
Expand Down
27 changes: 0 additions & 27 deletions test/build/base-url-remote/base-url-remote.test.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/build/base-url-remote/package.json

This file was deleted.

Binary file removed test/build/base-url-remote/public/favicon.ico
Binary file not shown.
14 changes: 0 additions & 14 deletions test/build/base-url-remote/public/index.css

This file was deleted.

24 changes: 0 additions & 24 deletions test/build/base-url-remote/public/index.html

This file was deleted.

10 changes: 0 additions & 10 deletions test/build/base-url-remote/public/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions test/build/base-url-remote/snowpack.config.js

This file was deleted.

10 changes: 0 additions & 10 deletions test/build/base-url-remote/src/index.js

This file was deleted.

48 changes: 0 additions & 48 deletions test/build/base-url/base-url.test.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/build/base-url/package.json

This file was deleted.

24 changes: 0 additions & 24 deletions test/build/base-url/public/index.html

This file was deleted.

10 changes: 0 additions & 10 deletions test/build/base-url/public/index.js

This file was deleted.

9 changes: 0 additions & 9 deletions test/build/base-url/snowpack.config.js

This file was deleted.

13 changes: 0 additions & 13 deletions test/build/base-url/src/index.js

This file was deleted.

Binary file removed test/build/base-url/src/logo.png
Binary file not shown.
24 changes: 0 additions & 24 deletions test/build/base-url/web_modules/array-flatten.js

This file was deleted.

5 changes: 0 additions & 5 deletions test/build/base-url/web_modules/import-map.json

This file was deleted.

1 change: 0 additions & 1 deletion test/build/config-out-flag/.gitignore

This file was deleted.

19 changes: 0 additions & 19 deletions test/build/config-out-flag/config-out-flag.test.js

This file was deleted.

Loading

1 comment on commit 18b9f09

@vercel
Copy link

@vercel vercel bot commented on 18b9f09 Apr 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.