Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Refactor lib/plugins/aws/package/compile/events/cloudFront/index.test.js #8528

Open
4 tasks
medikoo opened this issue Nov 20, 2020 · 2 comments
Open
4 tasks

Comments

@medikoo
Copy link
Member

@medikoo medikoo commented Nov 20, 2020

Most of the tests that currently cover core functionalities depend heavily on (and sometimes test) internal implementation characteristics. While they should be testing whether given implementation produces desired outcome (treating its implementation more as a black box).

Current state of things is problematic for eventual internal improvements and refactors which occasionally we want to introduce, as in most of such cases proposed improvements need to be accompanied with counterproductive numerous updates to tests which are covering otherwise not altered functionalities .

At some point we've introduced a new (black box based) way of testing the internals. It's through runServerless utlity, which allows us create natural (as in real world) serverless instance, and inspect the produced outcome for chosen command. More details here: https://github.com/serverless/serverless/tree/master/test#unit-tests


This is issue is about refactoring lib/plugins/aws/package/compile/events/cloudFront/index.test.js to runServerless based variant.

It's needed, so we cleanly move forward with #8502

To make refactor relatively easy:

  • Spec for new tests is fully defined and is already committed in. It's in scope of this describe block:
    describe('lib/plugins/aws/package/compile/events/cloudFront/index.new.test.js', () => {
  • Each test slot indicates which test it is about to replace
  • It is indicated where (and how many) exactly runServerless runs are expected to happen, against which fixture and command they should be based
  • Necessary hints are provided

For every refactored test, the old test should be removed

Refactor can be submitted with one PR, but partial refactors that cover some of below listed parts are also very welcome:

  • Removal notice:
    describe.skip('TODO: Removal notice', () => {
    it('should show preconfigured notice on "sls remove" if service has cloudFront event', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L88-L109
    // Inspect result.stdoutData
    await runServerless({
    fixture: 'function',
    cliArgs: ['remove'],
    lastLifecycleHookName: 'before:remove:remove',
    });
    });
    it('should not show preconfigured notice on "sls remove" if service doesn\'t have cloudFront event', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L113-L118
    // Inspect result.stdoutData
    await runServerless({
    fixture: 'function',
    cliArgs: ['remove'],
    lastLifecycleHookName: 'before:remove:remove',
    });
    });
    });
  • Validation:
    describe('Validation', () => {
    it.skip('TODO: should throw if function `memorySize` is greater than 128 for `functions[].events.cloudfront.evenType: "viewer-request"`', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L131-L167
    return expect(
    runServerless({ fixture: 'function', cliArgs: ['package'] })
    ).to.eventually.be.rejected.and.have.property('code', 'TODO');
    // Ensure ServerlessError is thrown and that it has some meaningful code
    // Then test like here:
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/httpApi/index.test.js#L455-L458
    });
    it.skip('TODO: should throw if function `timeout` is greater than 5 for for `functions[].events.cloudfront.evenType: "viewer-request"', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L169-L204
    return expect(
    runServerless({ fixture: 'function', cliArgs: ['package'] })
    ).to.eventually.be.rejected.and.have.property('code', 'TODO');
    });
    it.skip('TODO: should throw if function `memorySize` is greater than 3008 for `functions[].events.cloudfront.evenType: "origin-request"`', async () => {
    // Replaces partially
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L206-L242
    return expect(
    runServerless({ fixture: 'function', cliArgs: ['package'] })
    ).to.eventually.be.rejected.and.have.property('code', 'TODO');
    });
    it.skip('TODO: should throw if function `memorySize` is greater than 3008 for `functions[].events.cloudfront.evenType: "origin-response"`', async () => {
    // Replaces partially
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L206-L242
    return expect(
    runServerless({ fixture: 'function', cliArgs: ['package'] })
    ).to.eventually.be.rejected.and.have.property('code', 'TODO');
    });
    it.skip('TODO: should throw if function `timeout` is greater than 30 for `functions[].events.cloudfront.evenType: "origin-request"`', async () => {
    // Replaces partially
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L244-L280
    return expect(
    runServerless({ fixture: 'function', cliArgs: ['package'] })
    ).to.eventually.be.rejected.and.have.property('code', 'TODO');
    });
    it.skip('TODO: should throw if function `timeout` is greater than 30 for `functions[].events.cloudfront.evenType: "origin-response"`', async () => {
    // Replaces partially
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L244-L280
    return expect(
    runServerless({ fixture: 'function', cliArgs: ['package'] })
    ).to.eventually.be.rejected.and.have.property('code', 'TODO');
    });
    it.skip('TODO: should throw an error if the region is not us-east-1', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L355-L430
    return expect(
    runServerless({ fixture: 'function', cliArgs: ['package'] })
    ).to.eventually.be.rejected.and.have.property('code', 'TODO');
    });
    it.skip('TODO: should throw if more than one cloudfront event with different origins were defined as a default', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L988-L1034
    return expect(
    runServerless({ fixture: 'function', cliArgs: ['package'] })
    ).to.eventually.be.rejected.and.have.property('code', 'TODO');
    });
    it.skip('TODO: should throw if none of the cloudfront events with different origins were defined as a default', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L1308-L1369
    return expect(
    runServerless({ fixture: 'function', cliArgs: ['package'] })
    ).to.eventually.be.rejected.and.have.property('code', 'TODO');
    });
  • Alternative cases:
    describe.skip('TODO: Alternative cases', () => {
    it('should not create cloudfront distribution when no cloudFront events are given', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L572-L593
    await runServerless({ fixture: 'function', cliArgs: ['package'] });
    });
    it('should create DefaultCacheBehavior if there are no events without PathPattern configured', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L1133-L1197
    await runServerless({ fixture: 'function', cliArgs: ['package'] });
    });
    it('should throw if more than one origin with the same PathPattern', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L1519-L1577
    await runServerless({ fixture: 'function', cliArgs: ['package'] });
    });
    it('should throw if more than one origin with the same event type', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L1579-L1640
    await runServerless({ fixture: 'function', cliArgs: ['package'] });
    });
    });
  • Resource generation:
    it.skip('TODO: should configure needed resources', () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L432-L570
    });
    it.skip('TODO: should ensure that triggered functions are versioned', () => {
    // Replaces partially
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L283-L315
    });
    it.skip('TODO: should ensure that triggered functions have 128MB as default `memorySize`', () => {
    // Replaces partially
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L283-L315
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L317-L352
    });
    it.skip('TODO: should ensure that triggered functions have 5s for default `timeout`', () => {
    // Replaces partially
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L283-L315
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L317-L352
    });
    it.skip('TODO: should create different origins for different domains with the same path', () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L595-L663
    });
    it.skip('TODO: should create different origins for the same domains with the same path but different protocols', () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L665-L735
    });
    it.skip('TODO: should create different origins with different ids for different domains in the same function', () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L737-L794
    });
    it.skip('TODO: should use previous created origin for the same params', () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L796-L853
    });
    it.skip('TODO: should support origin customization', () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L855-L901
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L903-L986
    });
    it.skip('TODO: should assign a DefaultCacheBehavior behavior to event without PathPattern', () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L1036-L1131
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L1371-L1453
    });
    it.skip('TODO: should create DefaultCacheBehavior if there are no events without PathPattern configured and isDefaultOrigin flag was set', async () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L1199-L1306
    });
    it.skip('TODO: should support behavior customization', () => {
    // Replaces
    // https://github.com/serverless/serverless/blob/85e480b5771d5deeb45ae5eb586723c26cf61a90/lib/plugins/aws/package/compile/events/cloudFront/index.test.js#L1455-L1517
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants