1 | const path = require("path");
|
2 | const webpackConfig = require("../src");
|
3 | const cwd = process.cwd();
|
4 |
|
5 | describe("用空参数初始化配置", () => {
|
6 | let config = webpackConfig();
|
7 |
|
8 | it("config是一个对象", () => {
|
9 | expect(typeof config).toBe("object");
|
10 | });
|
11 |
|
12 | it("context需要指向项目根目录", () => {
|
13 | expect(config.context).toBe(process.cwd());
|
14 | });
|
15 |
|
16 | it("entry要指向./index.js", () => {
|
17 | expect(config.entry).toBe("./index.js");
|
18 | });
|
19 | it("alias & 需要指向根目录", () => {
|
20 | expect(config.resolve.alias).toEqual({
|
21 | "&": cwd,
|
22 | _: cwd
|
23 | });
|
24 | });
|
25 | it('mode应该等于development', () => {
|
26 | expect(config.mode).toBe('development')
|
27 | })
|
28 | });
|
29 |
|
30 | describe("带参数初始化配置", () => {
|
31 | let config = webpackConfig({
|
32 | entry: "./entry.js",
|
33 | moduleScope: "./src",
|
34 | alias: {
|
35 | "@": "xxxx",
|
36 | _: cwd
|
37 | },
|
38 | mode: 'production'
|
39 | });
|
40 |
|
41 | it("context需要始终保持在根目录,以防止dllcontext不一致", () => {
|
42 | expect(config.context).toBe(path.resolve(cwd));
|
43 | });
|
44 |
|
45 | it("alias得正确的合并", () => {
|
46 | expect(config.resolve.alias).toEqual({
|
47 | "&": path.resolve(cwd, "./src"),
|
48 | "@": "xxxx",
|
49 | "_": cwd
|
50 | });
|
51 | });
|
52 |
|
53 | it("如果提供applyPlugins测,那么applyPlugins方法应该被调用,并且参数是一个非空数组,同时config.plugins的值应该等于applyPlugins方法的返回值", ()=> {
|
54 | let newPlugins = [];
|
55 | let spy = jest.fn();
|
56 | spy.mockReturnValue(newPlugins);
|
57 | let config = webpackConfig({
|
58 | applyPlugins: spy
|
59 | });
|
60 | expect(spy).toHaveBeenCalledTimes(1);
|
61 | expect(spy).toBeCalledWith(expect.any(Array));
|
62 | expect(config.plugins === newPlugins).toBe(true);
|
63 | })
|
64 |
|
65 | it("如果提供applyRules参数,那么applyRule方法应该被调用,并且参数是一个非空数组,同时config.modules.rules的值应该等于applyRules方法的返回值", () => {
|
66 | let newRules = [];
|
67 | let spy = jest.fn();
|
68 | spy.mockReturnValue(newRules);
|
69 | let config = webpackConfig({
|
70 | applyRules: spy
|
71 | })
|
72 | expect(spy).toHaveBeenCalledTimes(1);
|
73 | expect(spy).toBeCalledWith(expect.any(Array));
|
74 | expect(config.module.rules === newRules).toBe(true);
|
75 | })
|
76 | it('mode言等于production', () => {
|
77 | expect(config.mode).toBe('production')
|
78 | })
|
79 | });
|