Skip to content

Commit aec6b58

Browse files
committed
Expand upon and add coverage for throws on invalid export type
1 parent fe1e3c6 commit aec6b58

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

__tests__/index.spec.js

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ describe('js-to-styles-vars-loader', () => {
2323
spyOn(operator, 'getResource').and.callThrough();
2424
loader.call(context, 'asdf');
2525
expect(operator.getResource).toHaveBeenCalledWith(context);
26-
2726
});
2827

2928
it('calls getPreprocessorType with resource', () => {
@@ -54,7 +53,23 @@ describe('js-to-styles-vars-loader', () => {
5453
});
5554
});
5655

57-
56+
describe('guardExportType', () => {
57+
it ("throws on anything except an object, does not throw otherwise", () => {
58+
const areOk = [{}, {a: "foo"}];
59+
const areNotOk = [[], ["a"], "", "123", 123, false, true];
60+
expect(() => {
61+
for (const okThing of areOk) {
62+
operator.guardExportType(okThing, "");
63+
}
64+
}).not.toThrow();
65+
for (const okThing of areNotOk) {
66+
expect(() => {
67+
operator.guardExportType(okThing, "");
68+
}).toThrow();
69+
}
70+
71+
})
72+
});
5873

5974
describe('getVarData', () => {
6075
const context = {
@@ -75,6 +90,51 @@ describe('js-to-styles-vars-loader', () => {
7590
const varData = operator.getVarData(path.join(context.context, './mocks/corners.js'), 'deep.nested');
7691
expect(varData).toEqual({ color: '#f00'});
7792
});
93+
94+
it('throws on an missing module', () => {
95+
expect(() => {
96+
operator.getVarData(path.join(context.context, './mocks/this_is_not_an_existing_file.js'));
97+
}).toThrow();
98+
})
99+
it('throws on a non-object export', () => {
100+
expect(() => {
101+
operator.getVarData(path.join(context.context, './mocks/null_export.js'));
102+
}).toThrow();
103+
})
104+
105+
106+
107+
it('throws on an empty property', () => {
108+
expect(() => {
109+
operator.getVarData(path.join(context.context, './mocks/bad_exports.js'), 'empty');
110+
}).toThrow();
111+
expect(() => {
112+
operator.getVarData(path.join(context.context, './mocks/bad_exports.js'), 'notEmptyObject');
113+
}).not.toThrow();
114+
})
115+
116+
it('does not throw on an empty object', () => {
117+
expect(() => {
118+
operator.getVarData(path.join(context.context, './mocks/bad_exports.js'), 'emptyObject');
119+
}).not.toThrow();
120+
})
121+
122+
it('throws on a non-object property', () => {
123+
expect(() => {
124+
operator.getVarData(path.join(context.context, './mocks/bad_exports.js'), 'falsey');
125+
}).toThrow();
126+
expect(() => {
127+
operator.getVarData(path.join(context.context, './mocks/bad_exports.js'), 'truthy');
128+
}).toThrow();
129+
expect(() => {
130+
operator.getVarData(path.join(context.context, './mocks/bad_exports.js'), 'emptyArray');
131+
}).toThrow();
132+
expect(() => {
133+
operator.getVarData(path.join(context.context, './mocks/bad_exports.js'), 'nonEmptyArray');
134+
}).toThrow();
135+
136+
})
137+
78138
});
79139

80140
describe('transformToSassVars', () => {

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ const requireReg = /require\s*\((["'])([\w.\/]+)(?:\1)\)((?:\.[\w_-]+)*);?/igm;
66

77
const operator = {
88

9+
guardExportType (data, relativePath) {
10+
if (typeof data !== "object" || Array.isArray(data)) {
11+
throw new Error(`Value must be an object '${relativePath}'`)
12+
}
13+
},
14+
915
getVarData (relativePath, property) {
1016
const data = require(relativePath);
1117
decache(relativePath);
1218
if (!data) {
1319
throw new Error(`No data in '${relativePath}'`)
20+
this.guardExportType(data, relativePath);
1421
}
1522
if (property) {
1623
const propVal = squba(data, property);
17-
if (!propVal) {
18-
throw new Error(`Empty property: 'require("${relativePath}")${property}`);
19-
}
24+
this.guardExportType(propVal, relativePath);
2025
return propVal;
2126
}
2227
return data;

0 commit comments

Comments
 (0)