webpack/test
Tobias Koppers f25345cae9 refactor code generation and HMR plugin to generate real module hashes for HMR 2021-01-26 14:45:11 +01:00
..
__snapshots__ refactor code generation and HMR plugin to generate real module hashes for HMR 2021-01-26 14:45:11 +01:00
benchmarkCases
cases Merge pull request #12452 from webpack/bugfix/12044 2021-01-21 09:13:55 +01:00
configCases add commonjsMagicComments options 2021-01-20 20:19:49 +01:00
fixtures add test case for ESM tracking 2021-01-13 23:54:15 +01:00
helpers improve error handling 2021-01-21 21:05:23 +01:00
hotCases add lazy compilation for entrypoints 2021-01-22 01:01:17 +01:00
hotPlayground
statsCases add warning when invalid dependencies are reported by loaders/plugins 2021-01-07 13:31:16 +01:00
watchCases move responsibility of exports info and connected modules hashing from Dependencies to Module 2021-01-24 21:36:36 +01:00
AbstractMethodError.unittest.js
BenchmarkTestCases.benchmark.js require.requireActual -> jest.requireActual 2020-11-26 11:53:32 +01:00
BinaryMiddleware.unittest.js nitpicks 2020-12-14 23:05:40 +01:00
BuildDependencies.test.js add test case for ESM tracking 2021-01-13 23:54:15 +01:00
ChangesAndRemovals.test.js
Chunk.unittest.js
Cli.test.js replace output.ecmaVersion with output.environment and more detailed flags 2020-09-10 11:12:28 +02:00
Compiler-caching.test.js
Compiler.test.js refactor: update other instances 2021-01-19 17:19:33 +05:30
ConfigCacheTestCases.test.js add "snapshot" configuration to allow to configure how snapshots are created 2020-08-26 01:27:13 +02:00
ConfigTestCases.template.js only return result of startup when needed 2021-01-18 19:21:12 +01:00
ConfigTestCases.test.js
ContextModuleFactory.unittest.js fix bug for maxCallStack in ContextModuleFactory 2020-09-13 18:13:52 +05:30
Defaults.unittest.js add commonjsMagicComments options 2021-01-20 20:19:49 +01:00
Errors.test.js improve and align error message 2021-01-18 16:22:07 +01:00
Examples.test.js increase timeouts 2020-09-01 09:12:53 +02:00
HotModuleReplacementPlugin.test.js fix test case 2020-11-28 18:27:44 +01:00
HotTestCases.template.js add experimental lazy compilation 2021-01-21 17:39:56 +01:00
HotTestCasesAsyncNode.test.js
HotTestCasesNode.test.js
HotTestCasesWeb.test.js
HotTestCasesWebWorker.test.js
JavascriptParser.unittest.js run prettier 2020-09-01 11:30:46 +02:00
LocalModulesHelpers.unittest.js
ModuleDependencyError.unittest.js
MultiCompiler.test.js refactor: use es6 arrow functions for the sake of consistency 2021-01-19 16:56:14 +05:30
MultiStats.test.js
MultiWatching.unittest.js
NodeTemplatePlugin.test.js
NormalModule.unittest.js add useSimpleSourceMap flag to enable/disable OriginalSource for generated code 2020-10-26 16:33:38 +01:00
NullDependency.unittest.js
PersistentCaching.test.js fix timeout in PersistentCaching test 2020-07-08 11:12:58 +02:00
ProfilingPlugin.test.js add test case 2021-01-20 17:54:53 +01:00
ProfilingPlugin.unittest.js
ProgressPlugin.test.js do cache related tasks after calling handler in watching 2020-08-01 09:00:17 +02:00
README.md update NodeJS version 2020-08-18 22:41:14 +08:00
RawModule.unittest.js
RequestShortener.unittest.js
SemVer.unittest.js run prettier 2020-09-01 11:30:46 +02:00
SideEffectsFlagPlugin.unittest.js
SizeFormatHelpers.unittest.js
SortableSet.unittest.js
Stats.test.js re-add chunkId to script loading when available 2021-01-06 10:08:40 +01:00
StatsTestCases.test.js replace hash, version, time and builtAt with build summary at the bottom 2020-09-03 22:12:52 +02:00
Template.unittest.js
TestCases.template.js fix caching crash in ConcatenatedModule 2021-01-08 15:44:26 +01:00
TestCasesAllCombined.test.js remove more expected deprecations 2020-08-04 14:54:06 +02:00
TestCasesCachePack.test.js add "snapshot" configuration to allow to configure how snapshots are created 2020-08-26 01:27:13 +02:00
TestCasesDevelopment.test.js
TestCasesDevtoolCheapSourceMap.test.js
TestCasesDevtoolEval.test.js
TestCasesDevtoolEvalCheapModuleSourceMap.test.js
TestCasesDevtoolEvalCheapSourceMap.test.js
TestCasesDevtoolEvalDeterministicModuleIds.test.js
TestCasesDevtoolEvalNamedModules.test.js
TestCasesDevtoolEvalSourceMap.test.js
TestCasesDevtoolInlineCheapSourceMap.test.js
TestCasesDevtoolInlineSourceMap.test.js
TestCasesDevtoolSourceMap.test.js
TestCasesHot.test.js
TestCasesMinimizedSourceMap.test.js remove more expected deprecations 2020-08-04 14:54:06 +02:00
TestCasesNormal.test.js
TestCasesProdGlobalUsed.test.js build chunk graph with runtime awareness 2020-10-16 11:47:38 +02:00
TestCasesProduction.test.js improve optional chaining handling 2020-08-07 14:45:25 +02:00
URLAbsoluteSpecifier.unittest.js improve windows absolute path detection 2020-07-03 10:18:41 +02:00
Validation.test.js add commonjsMagicComments options 2021-01-20 20:19:49 +01:00
WatchClose.test.js improve code and test 2020-07-15 14:48:40 +02:00
WatchDetection.test.js
WatchSuspend.test.js
WatchTestCases.test.js move responsibility of exports info and connected modules hashing from Dependencies to Module 2021-01-24 21:36:36 +01:00
WatcherEvents.test.js
WebpackError.unittest.js
checkArrayExpectation.js
cleverMerge.unittest.js improve handling of non-object values in clever merge 2021-01-06 10:51:44 +01:00
compareLocations.unittest.js
compileBooleanMatcher.unittest.js fix #12255 2020-12-22 12:29:08 +01:00
extractUrlAndGlobal.unittest.js fix ExternalModule bug 2020-08-11 20:42:32 +08:00
formatLocation.unittest.js
identifier.unittest.js
numberHash.unittest.js
objectToMap.unittest.js
setupTestFramework.js
smartGrouping.unittest.js add grouping of assets 2020-09-01 18:16:43 +02:00
target-browserslist.unittest.js fix: more 2020-11-02 18:21:37 +03:00

README.md

Welcome to the webpack test suite!!!!

Every pull request that you submit to webpack (besides README and spelling corrections in comments) requires tests that are created.

But don't give up hope!!! Although our tests may appear complex and overwhelming, once you become familiar with the test suite and structure, adding and creating tests will be fun and beneficial as you work inside the codebase! ❤

tl;dr

Run all tests (this automatically runs the setup):

yarn test

Run an individual suite:

yarn jest ConfigTestCases

Watch mode:

yarn jest --watch ConfigTestCases

See also: Jest CLI docs

Test suite overview

We use Jest for our tests. For more information on Jest you can visit their homepage!

Class Tests

All test files can be found in *.test.js. There are many tests that simply test API's of a specific class/file (such as Compiler, Errors, Integration, Parser, RuleSet, Validation). If the feature you are contributing involves one of those classes, then best to start there to understand the structure.

xCases

In addition to Class specific tests, there are also directories that end in "Cases". The suites for these cases also have corresponding *.test.js files.

cases (TestCases.test.js) 1

Cases are a set of general purpose tests that will run against a variety of permutations of webpack configurations. When you are making a general purpose change that doesn't require you to have a special configuration, you would likely add your tests here. Inside of the ./test/cases directory you will find tests are broken into thematic sub directories. Take a moment to explore the different options.

To add a new case, create a new directory inside of the top level test groups, and then add an index.js file (and any other supporting files).

By default this file will be the entry point for the test suite and you can add your it()'s there. This will also become bundled so that node env support happens as well.

configCases (ConfigTestCases.test.js) 1

If you are trying to solve a bug which is reproducible when x and y properties are used together in a config, then configCases is the place to be!!!!

In addition to an index.js, these configCases require a webpack.config.js is located inside of your test suite. This will run this specific config through webpack just as you were building individually. They will use the same loading/bundling technique of your it() tests, however you now have a more specific config use cases that you can write even before you start coding.

statsCases (StatsTestCases.test.js)

Stats cases are similar to configCases except specifically focusing on the expected output of your stats. Instead of writing to the console, however the output of stats will be written to disk.

By default, the "expected" outcome is a pain to write by hand so instead when statsCases are run, runner is checking output using jest's awesome snapshot functionality.

Basically you don't need to write any expected behaviors yourself. The assumption is that the stats output from your test code is what you expect.

Please follow the approach described below:

  • write your test code in statsCases/ folder by creating a separate folder for it, for example statsCases/some-file-import-stats/index.js
import("./someModule");
  • don't forget the webpack.config.js
  • run the test
  • jest will automatically add the output from your test code to StatsTestCases.test.js.snap and you can always check your results there
  • Next time test will run -> runner will compare results against your output written to snapshot previously

You can read more about SnapShot testing right here

Questions? Comments?

If you are still nervous or don't quite understand, please submit an issue and tag us in it, and provide a relevant PR while working on!

Footnotes

1 webpack's parser supports the use of ES2015 features like arrow functions, harmony exports, etc. However as a library we follow NodeJS's timeline for dropping older versions of node. Because of this we expect your tests on Travis to pass all the way back to NodeJS v10; Therefore if you would like specific tests that use these features to be ignored if they are not supported, then you should add a test.filter.js file. This allows you to import the syntax needed for that test, meanwhile ignoring it on node versions (during CI) that don't support it. webpack has a variety of helpful examples you can refer to if you are just starting out. See the ./helpers folder to find a list of the versions.