Skip to content

Commit 68e60dc

Browse files
committed
test: adds coverage for esbuild features
1 parent 571382c commit 68e60dc

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed

app-config-esbuild/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
},
3232
"dependencies": {
3333
"@app-config/config": "^2.7.2",
34-
"@app-config/node": "^2.7.2",
3534
"@app-config/utils": "^2.7.2"
3635
},
3736
"devDependencies": {
37+
"@app-config/test-utils": "^2.7.2",
3838
"esbuild": "0.13"
3939
},
4040
"prettier": "@lcdev/prettier",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`loads config correctly 1`] = `
4+
"(()=>{var r={foo:\\"bar\\"},e=(typeof window==\\"undefined\\"?globalThis:window)||{},t=e._appConfig||r;(typeof e._appConfig==\\"undefined\\"||!Object.isFrozen(e._appConfig))&&(e._appConfig=t);var a=t;console.log(a);})();
5+
"
6+
`;
7+
8+
exports[`loads currentEnvironment 1`] = `
9+
"(()=>{var t={foo:\\"bar\\"},e=(typeof window==\\"undefined\\"?globalThis:window)||{},r=e._appConfig||t;(typeof e._appConfig==\\"undefined\\"||!Object.isFrozen(e._appConfig))&&(e._appConfig=r);function o(){return\\"test\\"}console.log(o());})();
10+
"
11+
`;
12+
13+
exports[`loads validation function 1`] = `
14+
"(()=>{var v={foo:\\"bar\\"},n=(typeof window==\\"undefined\\"?globalThis:window)||{},m=n._appConfig||v;(typeof n._appConfig==\\"undefined\\"||!Object.isFrozen(n._appConfig))&&(n._appConfig=m);function P(){let s={};return function(p){\\"use strict\\";p.exports=i,p.exports.default=i;var b={type:\\"object\\",additionalProperties:!1,properties:{foo:{type:\\"string\\"}},$schema:\\"http://json-schema.org/draft-07/schema#\\"};function i(o,t){\\"use strict\\";if(t)var a=t.dataPath,g=t.parentData,h=t.parentDataProperty,y=t.rootData;else var a=\\"\\",g=void 0,h=void 0,y=o;var e=null,r=0;if(o&&typeof o==\\"object\\"&&!Array.isArray(o)){for(var f in o)if(f!==\\"foo\\"){var d={keyword:\\"additionalProperties\\",dataPath:a,schemaPath:\\"#/additionalProperties\\",params:{additionalProperty:f},message:\\"should NOT have additional properties\\"};e===null?e=[d]:e.push(d),r++}if(o.foo!==void 0&&typeof o.foo!=\\"string\\"){var l={keyword:\\"type\\",dataPath:a+\\"/foo\\",schemaPath:\\"#/properties/foo/type\\",params:{type:\\"string\\"},message:\\"should be string\\"};e===null?e=[l]:e.push(l),r++}}else{var c={keyword:\\"type\\",dataPath:a,schemaPath:\\"#/type\\",params:{type:\\"object\\"},message:\\"should be object\\"};e===null?e=[c]:e.push(c),r++}return i.errors=e,r===0}}(s),s.exports}var u=P();u({foo:12});})();
15+
"
16+
`;

app-config-esbuild/src/index.test.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { withTempFiles } from '@app-config/test-utils';
2+
import { build } from 'esbuild';
3+
import createPlugin from './index';
4+
5+
it('loads config correctly', () =>
6+
withTempFiles(
7+
{
8+
'.app-config.schema.yml': `
9+
type: object
10+
additionalProperties: true
11+
`,
12+
'.app-config.yml': `
13+
foo: bar
14+
`,
15+
'a.js': `
16+
import config from '@app-config/main';
17+
console.log(config);
18+
`,
19+
},
20+
async (inDir) => {
21+
const res = await build({
22+
entryPoints: [inDir('a.js')],
23+
plugins: [createPlugin({ loadingOptions: { directory: inDir('.') } })],
24+
bundle: true,
25+
minify: true,
26+
write: false,
27+
});
28+
29+
expect(res.outputFiles[0].text).toMatchSnapshot();
30+
},
31+
));
32+
33+
it('fails when config is incorrect', () =>
34+
withTempFiles(
35+
{
36+
'.app-config.schema.yml': `
37+
type: object
38+
additionalProperties: false
39+
`,
40+
'.app-config.yml': `
41+
foo: bar
42+
`,
43+
'a.js': `
44+
import config from '@app-config/main';
45+
console.log(config);
46+
`,
47+
},
48+
async (inDir) => {
49+
await expect(
50+
build({
51+
entryPoints: [inDir('a.js')],
52+
plugins: [createPlugin({ loadingOptions: { directory: inDir('.') } })],
53+
bundle: true,
54+
minify: true,
55+
write: false,
56+
}),
57+
).rejects.toThrow(
58+
'error: [plugin: @app-config/esbuild] Config is invalid: config should NOT have additional properties',
59+
);
60+
},
61+
));
62+
63+
it('loads validation function', () =>
64+
withTempFiles(
65+
{
66+
'.app-config.schema.yml': `
67+
type: object
68+
additionalProperties: false
69+
properties:
70+
foo: { type: string }
71+
`,
72+
'.app-config.yml': `
73+
foo: bar
74+
`,
75+
'a.js': `
76+
import { validateConfig } from '@app-config/main';
77+
78+
validateConfig({ foo: 12 })
79+
`,
80+
},
81+
async (inDir) => {
82+
const res = await build({
83+
entryPoints: [inDir('a.js')],
84+
plugins: [createPlugin({ loadingOptions: { directory: inDir('.') } })],
85+
bundle: true,
86+
minify: true,
87+
write: false,
88+
});
89+
90+
expect(res.outputFiles[0].text).toMatchSnapshot();
91+
},
92+
));
93+
94+
it('loads currentEnvironment', () =>
95+
withTempFiles(
96+
{
97+
'.app-config.schema.yml': `
98+
type: object
99+
additionalProperties: false
100+
properties:
101+
foo: { type: string }
102+
`,
103+
'.app-config.yml': `
104+
foo: bar
105+
`,
106+
'a.js': `
107+
import { currentEnvironment } from '@app-config/main';
108+
109+
console.log(currentEnvironment())
110+
`,
111+
},
112+
async (inDir) => {
113+
const res = await build({
114+
entryPoints: [inDir('a.js')],
115+
plugins: [createPlugin({ loadingOptions: { directory: inDir('.') } })],
116+
bundle: true,
117+
minify: true,
118+
write: false,
119+
});
120+
121+
expect(res.outputFiles[0].text).toMatchSnapshot();
122+
},
123+
));

0 commit comments

Comments
 (0)