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

Only update sources to be mutated in the sandbox #2407

Closed
brodybits opened this issue Aug 21, 2020 · 2 comments
Closed

Only update sources to be mutated in the sandbox #2407

brodybits opened this issue Aug 21, 2020 · 2 comments
Labels
🚀 Feature request New feature request
Milestone

Comments

@brodybits
Copy link

brodybits commented Aug 21, 2020

Is your feature request related to a problem? Please describe.

One part of issue #2403 is that when we add file headers and strip comments, it leads to issues between the React library (https://github.com/facebook/react) and Jest. This can be solved by excluding the JavaScript sources, like I proposed in #2406, but it got me thinking about a more general solution.

If we would only update sources to be mutated in the sandbox, and keep the other sources as-is, it may avoid this kind of an issue with some other projects in the future.

Describe the solution you'd like

rework the sandbox setup as follows:

  • get all source files, like Stryker already does
  • only match files to mutate with the sandbox patterns
  • if a file both is in the files-to-mutate list and matches sandbox.fileHeaders pattern, then add the header
  • if a file both is in the files-to-mutate list and matches sandbox.stripComments pattern, then strip the comments

Describe alternatives you've considered

updated:

Additional context

I tried working on this idea myself. I think this should be pretty straightforward with Injector from typed-inject but do not yet really understand how to work with the typed-inject API.


P.S. After some testing, I think this would not solve the problem with the React library. So this idea may not be worth considering for the near future.

@nicojs
Copy link
Member

nicojs commented Aug 25, 2020

Hi @brodybits once again. I overlooked this issue, sorry for the late(r) reply.

If we would only update sources to be mutated in the sandbox, and keep the other sources as-is, it may avoid this kind of an issue with some other projects in the future.

Unfortunately, this won't work for the typescript use case, as stated in #2415

For example:

// concat.ts
function concat(a: string, b: string) {
   return a + b; // return type of this function will change because of mutation switching
}
// __tests__/concat.spec.ts
it('should concat', () => {
  const actual: string = concat('a', 'b');
  expect(actual).eq('ab');
});

In this case, __tests__/concat.spec.ts will give a compile error. You would notice it if you're using buildCommand: "tsc -b" or you use jest-ts for example, the test runner that supports type checking.

The solution I came up with is to prefix all javascript-and-friend files in the sandbox with // @ts-nocheck. That way, you'll disable type checking all together (works from TS3.7~ish). The nice thing is that this works for all typescript use cases.

Unfortunately, other // @ts-xxx directives can still interfere with // @ts-nocheck, that's why I've decided to also add the "stripComments" (or "removeComments") preprocessor. It removes all other comments from a file.

Both these preprocessors are on by default, because I thought that removing comments is usually safe to do. But then again... this is JavaScript we're talking about 😉.

think this would not solve the problem with the React library. So this idea may not be worth considering for the near future.

I'm really curious to know what the root cause of these issues is. I know that you can override jest's test environment with the /** @jest-environment */ directive, but you are alluding to something todo with the react library itself.

@nicojs
Copy link
Member

nicojs commented Aug 28, 2020

Closing this issue in favor of #2438

@nicojs nicojs closed this as completed Aug 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🚀 Feature request New feature request
Projects
None yet
Development

No branches or pull requests

2 participants