teambit / bit

A build system for development of composable software.
https://bit.dev
Other
17.86k stars 925 forks source link

Tests passed on local but failed to build #1384

Closed eddeee888 closed 5 years ago

eddeee888 commented 5 years ago

Context

Is it normal to be able to pass tests on local but when exported? It also mentions here that all tests must pass to successfully tag. I have created, tested then exported a simple component but it keeps failing on remote repo

Expected Behavior

Actual Behavior

screen shot 2019-01-21 at 10 57 37 pm screen shot 2019-01-21 at 10 58 48 pm

Steps to Reproduce the Problem

  1. Create file + test file
  2. Add compiler and test
  3. Add component: bit add src/forms/validations/emailValidation.ts -t 'src/forms/validations/emailValidation.test.ts' --id forms/validations
  4. Tag component: bit tag forms/validations 0.0.1 --message "Add email validation"
  5. Export component: bit export <scopename>

Files

.bitmap

{
    "eddeee888.learnd-utils/forms/validations@0.0.1": {
        "files": [
            {
                "relativePath": "src/forms/validations/emailValidation.test.ts",
                "test": true,
                "name": "emailValidation.test.ts"
            },
            {
                "relativePath": "src/forms/validations/emailValidation.ts",
                "test": false,
                "name": "emailValidation.ts"
            }
        ],
        "mainFile": "src/forms/validations/emailValidation.ts",
        "origin": "AUTHORED"
    },
    "version": "13.0.4"
}

bit.json

{
    "env": {
        "compiler": "bit.envs/compilers/typescript@0.0.5",
        "tester": "bit.envs/testers/jest@0.0.35"
    },
    "dependencies": {},
    "componentsDefaultDirectory": "components/{namespace}/{name}",
    "packageManager": "yarn"
}

Specifications

davidfirst commented 5 years ago

@eddeee888 , thanks for the detailed info. In most cases, yes, your local and the remote CI should behave the same. However, in the current version the tests you run locally rely on your workspace configuration and not on Bit configuration. So, it's possible that the tests pass on your local but fail on the remote. You can also try importing your components on another directory and test them there, it'll fail with the same error you got from the remote. We are working on a solution to fix this by having the same procedure on the local and on the remote.

According to your specific error, it seemed to me that the await wasn't used correctly. Eliminating entirely the async/await, fixes the issue. Probably the function doesn't return a Promise.

Here is the fixed version of the tests:

import email from './emailValidation';

describe('emailValidation()', () => {
  const valid = 'abc@gmail.com';
  const invalidWrongFormat = 'asdsadasd';

  it('emailValidation - valid', () => {
    expect(email.validate(valid)).resolves.not.toThrow();
    expect(email.validate(valid)).resolves.toBe(valid);
  });

  it('emailValidation - invalid: wrong email format', () => {
    expect(email.validate(invalidWrongFormat)).rejects.toThrow();
  });

  it('emailValidation - invalid: no email', () => {
    expect(email.validate(undefined)).rejects.toThrow();
  });

  it('emailValidation - invalid: empty string', () => {
    expect(email.validate('')).rejects.toThrow();
  });
});
eddeee888 commented 5 years ago

Thanks for the explanation!

Eliminating async/await passed but it's because the async errors are not caught in jest (version 23.x in this case). In this particular example, if an invalid email such as abcmail.com is used to test, the test would still pass, which is not what we want.

I went and look again at how to test promises using async/await. I can confirm async/await syntax does not work on remote as of now.

So to test promises, we can use the non async/await syntax like following:

import email from './emailValidation';

describe('emailValidation()', () => {
  const valid = 'abc@gmail.com';
  const invalidWrongFormat = 'asdsadasd';

  it('emailValidation - valid', () => {
    expect.assertions(1);

    return expect(email.validate(valid)).resolves.toBe(valid);
  });

  it('emailValidation - invalid: wrong email format', () => {
    expect.assertions(1);

    return expect(email.validate(invalidWrongFormat)).rejects.toThrow();
  });

  it('emailValidation - invalid: no email', () => {
    expect.assertions(1);

    return expect(email.validate(undefined)).rejects.toThrow();
  });

  it('emailValidation - invalid: empty string', () => {
    expect.assertions(1);

    return expect(email.validate('')).rejects.toThrow();
  });
});