Шаг 1: Установите пакет

# Yarn
yarn add -D babel-plugin-rewire
yarn add -D babel-jest
# NPM
npm install babel-plugin-rewire --save-dev
npm install babel-jest --save-dev

Шаг 2: Добавьте в конфигурацию babel

.babelrc

module.exports = {
  plugins: ['babel-plugin-rewire'],
};

Шаг 3. Проверьте настройки

# Yarn
yarn jest
# NPM
npm run test

Шаг 4: Напишите свою функцию Cloudfront

rewrite-url.js

function handler(event) {

    var request = event.request;

    // Extract the request from the CloudFront event
    var olduri = request.uri;

    // Match any '/?' that occurs and replace with 'index.html?'
    var newuri = olduri.replace(/\/\?/, '\/index.html?');

    // Replace the received URI with the new URI
    request.uri = newuri;

    return request
}

handler; // this is necessary for rewire

Шаг 5: Напишите файл модульного теста

rewrite-url.test.js

const originRequest = require('./rewrite-url')
const sut = originRequest.__get__('handler');
test('it does rewrite url case 1', async () => {
    
    // given
    const event = {
        request: {
            method: 'GET',
            uri: '/?code=123'
        }
    }

    // when
    const uri = sut(event).uri

    // then
    expect(uri).toBe('/index.html?code=123');
})

Шаг 6: Запустите тесты

# Yarn
yarn jest
# NPM
npm run test