-
Notifications
You must be signed in to change notification settings - Fork 809
/
assertions.js
140 lines (124 loc) · 3.89 KB
/
assertions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import File from '../../src/File';
/**
* Check that a matching webpack rule can be found
*
* @param {import("ava").Assertions} t
* @param {import("webpack").Configuration} config
* @param {(rule: import("webpack").RuleSetRule) => boolean} test
*/
function hasWebpackRule(config, test) {
/** @param {import("webpack").RuleSetRule} rule */
const checkRule = rule =>
test(rule) ||
(rule.oneOf || []).find(checkRule) ||
(rule.rules || []).find(checkRule);
return !!config.module.rules.find(checkRule);
}
/**
* Check that a matching rule with the given loader can be found
*
* @param {import("webpack").Configuration} config
* @param {string|(loader: string) => boolean} loader
*/
function hasWebpackLoader(config, loader) {
const checkLoader = typeof loader === 'string' ? str => str === loader : loader;
return hasWebpackRule(
config,
rule =>
(rule.loader && checkLoader(rule.loader)) ||
(rule.use || []).find(use => checkLoader(use.loader))
);
}
export default {
/**
* Assert that a matching webpack rule can be found
*
* @param {import("ava").Assertions} t
* @param {import("webpack").Configuration} config
* @param {(rule: import("webpack").RuleSetRule) => boolean} test
*/
hasWebpackRule: (t, config, test) => t.true(hasWebpackRule(config, test)),
/**
* Assert that a rule with the given loader can be found
*
* @param {import("ava").Assertions} t
* @param {import("webpack").Configuration} config
* @param {string|(loader: string) => boolean} loader
*/
hasWebpackLoader: (t, config, loader) => t.true(hasWebpackLoader(config, loader)),
/**
* Assert that a rule with the given loader cannot be found
*
* @param {import("ava").Assertions} t
* @param {import("webpack").Configuration} config
* @param {string|(loader: string) => boolean} loader
*/
doesNotHaveWebpackLoader: (t, config, loader) =>
t.false(hasWebpackLoader(config, loader)),
manifestEquals: (expected, t) => {
let manifest = JSON.parse(
File.find(`test/fixtures/app/dist/mix-manifest.json`).read()
);
t.deepEqual(Object.keys(manifest).sort(), Object.keys(expected).sort());
Object.keys(expected).forEach(key => {
t.true(new RegExp(expected[key]).test(manifest[key]));
});
},
/**
* Assert that a file exists
*
* @param {string} path
* @param {import("ava").Assertions} t
*/
fileExists: (path, t) => {
t.true(File.exists(path));
},
/**
* Assert that a file does not exist
*
* @param {string} path
* @param {import("ava").Assertions} t
*/
fileDoesNotExist: (path, t) => {
t.false(File.exists(path));
},
/**
* Assert that a file isn't empty.
*
* @param {string} path
* @param {import("ava").Assertions} t
*/
fileNotEmpty: (path, t) => {
t.not(File.find(path).read().replace(/\s/g, ''), '');
},
/**
* Assert that a file path matches the given CSS.
*
* @param {string} path
* @param {string} expected
* @param {import("ava").Assertions} t
*/
fileMatchesCss: (path, expected, t) => {
t.is(File.find(path).read().replace(/\s/g, ''), expected.replace(/\s/g, ''));
},
/**
* Assert that a file contains the given string
*
* @param {string} path
* @param {string} expected
* @param {import("ava").Assertions} t
*/
fileContains: (path, str, t) => {
t.true(new File(path).read().includes(str));
},
/**
* Assert that a file does not contain the given string
*
* @param {string} path
* @param {string} expected
* @param {import("ava").Assertions} t
*/
fileDoesNotContain: (path, str, t) => {
t.false(new File(path).read().includes(str));
}
};