serverless-bundle
serverless-bundle copied to clipboard
Stack traces not using source maps in Jest
The Problem
It appears that with the default configuration, source maps are not being used when running tests (via serverless-bundle test). This means that when an error is thrown, the generated stack trace and error messages don't display code or line numbers that are relevant to the actual code at hand.
An example of some code under test that intentionally generates an error:
for (const theVal of {}) {
console.log(theVal);
}
When running a test that calls this method, the generated error will look something like:
TypeError: Invalid attempt to iterate non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.
10 | }
11 | return {
> 12 | statusCode: 200,
| ^
13 | body: JSON.stringify({
14 | message: `Go Serverless v2.0! ${(await message({ time: 1, copy: 'Your function executed successfully!'}))}`,
15 | }),
at _createForOfIteratorHelper (handler.js:12:471)
at _callee$ (handler.js:7:22)
Possible Solution
I believe this can be resolved by updating the babelJestTransform.js file to include a target for the @babel/preset-env plugin like so: presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
Doing so will generate the following for the same failed test above:
TypeError: {} is not iterable
6 | */
7 | export const hello = async (event, context) => {
> 8 | for (const theVal of {}) {
| ^
9 | console.log(theVal);
10 | }
11 | return {
at Object.hello (handler.js:8:24)
at Object.<anonymous> (tests/handler.test.js:11:17)
Please let me know if you agree with the idea to include the node: 'current' target -- I'm not sure if there would be any other impact I'm not thinking about here. If so, I could make a PR with this change.
Thank you!