mercredi 6 janvier 2021

How to strip exports during build for GAS process? Need to use exports in local unit testing with Jest

Trying to use Jest for local unit & integration testing. I already have some npm scripts to build the GAS-compatible code and upload it with clasp so that's not an issue.

The problem is seemingly simple but I can't figure out the best way to handle removing the exports that Jest requires to work.

For example say I start with a super simple script like this in my sum.js file:

function sum(a, b) {
  return a + b
}

If I want to add a test to that function I add module.exports = sum which will result in a sum.js file like this:

function sum(a, b) {
  return a + b
}
module.exports = sum

And then write my tests in a sum.test.js file, something like this:

import { add } from "./index.js"

test('adds 1 + 2 to equal 3', () => {
  expect(add(1, 2)).toBe(3);
});

The problem is module.exports = sum and export { sum } create an exception in Google Apps Scripts (won't even upload with clasp), so I just want an easy way to remove all exports during the build process that's not a super hacky solution.

Aucun commentaire:

Enregistrer un commentaire