Skip to content

Commit 6361e1e

Browse files
authored
Merge pull request #6 from routablehq/bugfix/jest-28-error-invalid-return-type-with-process
[Bugfix] Fix Jest version 28.x.x throwing an Error for invalid return type in process handler
2 parents 8c64169 + 06585a8 commit 6361e1e

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/helpers.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
const fs = require('fs');
77
const crypto = require('crypto');
8+
// eslint-disable-next-line import/no-extraneous-dependencies
9+
const jest = require('jest');
810

911
const exportStartRegex = /\n:export.{\n*/;
1012
const exportEndRegex = /\n*}/;
@@ -43,3 +45,20 @@ module.exports.getCacheKey = () => (
4345
.update(fs.readFileSync(__filename))
4446
.digest('hex')
4547
);
48+
49+
module.exports.makeProcessExports = (exportString) => {
50+
// jest.getVersion() returns semantic version, like X.Y.Z
51+
// We are only interested in the major version (the "X") in this case.
52+
const [major] = jest.getVersion().split('.');
53+
const majorInt = parseInt(major, 10);
54+
55+
if (majorInt < 27) {
56+
// For jest versions 26.x.x and bellow, we are fine with just returning
57+
// the exports as a string
58+
return exportString;
59+
}
60+
61+
// For jest version 27.x.x and above, we want to return an object with a
62+
// "code" property, which contains an export string (see here: https://jestjs.io/docs/code-transformation)
63+
return { code: exportString };
64+
};

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const {
2525
hasExports,
2626
isSupportedFile,
2727
makeExportsString,
28+
makeProcessExports,
2829
parseExportsToObject,
2930
} = require('./helpers');
3031

@@ -50,7 +51,10 @@ module.exports = {
5051
}
5152

5253
// generate the module.exports string
53-
return makeExportsString(result);
54+
const exportString = makeExportsString(result);
55+
56+
// return the process export
57+
return makeProcessExports(exportString);
5458
},
5559
getCacheKey() {
5660
return getCacheKey();

src/index.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const fs = require('fs');
22
const path = require('path');
3+
const jest = require('jest');
34
const sinon = require('sinon');
45

56
const transformer = require('./index');
@@ -21,6 +22,10 @@ const emptyModuleString = 'module.exports = {};';
2122

2223
describe('jest-scss-transform', () => {
2324
describe('transformer.process', () => {
25+
beforeEach(() => {
26+
sinon.restore();
27+
});
28+
2429
it('returns empty module for file extensions other than .scss', () => {
2530
expect(transformer.process(contentsIncorrectExt, incorrectExtFile)).toBe(emptyModuleString);
2631
});
@@ -40,6 +45,14 @@ describe('jest-scss-transform', () => {
4045
it('processes files with poorly formatted exports', () => {
4146
expect(transformer.process(contentsFormatting, formattingFile)).toBe(expectedParsed);
4247
});
48+
49+
it.each(['27.0.0', '27.5.2', '28.0.0', '28.0.1'])(
50+
'returns object with code property for jest versions %s and above',
51+
(jestVersion) => {
52+
sinon.stub(jest, 'getVersion').returns(jestVersion);
53+
expect(transformer.process(contentsBasic, commentsFile)).toEqual({ code: expectedParsed });
54+
},
55+
);
4356
});
4457

4558
describe('transformer.getCacheKey', () => {

0 commit comments

Comments
 (0)