Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
178 views
in Technique[技术] by (71.8m points)

javascript - Logical OR for expected results in Jest

It will be the best explain in on example

expected(someNumber).toBe(1).or.toBe(-2).or.toBe(22) // expect result is 1 or -2 or 22

This is bad syntax, but can do sth like that in jest?

question from:https://stackoverflow.com/questions/44654210/logical-or-for-expected-results-in-jest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you really needed to do exactly that, I suppose you could put the logical comparisons inside the expect call, e.g.

expect(someNumber === 1 || someNumber === -2 || someNumber === 22).toBeTruthy();

If this is just for a "quick and dirty" check, this might suffice.

However, as suggested by several comments under your question, there seem to be several "code smells" that make both your initial problem as well as the above solution seem like an inappropriate way of conducting a test.

First, in terms of my proposed solution, that use of toBeTruthy is a corruption of the way Jasmine/Jest matchers are meant to be used. It's a bit like using expect(someNumber === 42).toBeTruthy(); instead of expect(someNumber).toBe(42). The structure of Jest/Jasmine tests is to provide the actual value in the expect call (i.e. expect(actualValue)) and the expected value in the matcher (e.g. toBe(expectedValue) or toBeTruthy() where expectedValue and true are the expected values respectively). In the case above, the actual value is (inappropriately) provided in the expect call, with the toBeTruthy matcher simply verifying this fact.

It might be that you need to separate your tests. For example, perhaps you have a function (e.g. called yourFunction) that you are testing that provides (at least) 3 different possible discrete outputs. I would presume that the value of the output depends on the value of the input. If that is the case, you should probably test all input/output combinations separately, e.g.

it('should return 1 for "input A" ', () => {
  const someNumber = yourFunction("input A");
  expect(someNumber).toBe(1);
});

it('should return -2 for "input B" ', () => {
  const someNumber = yourFunction("input B");
  expect(someNumber).toBe(-2);
});

it('should return 22 for "input C" ', () => {
  const someNumber = yourFunction("input C");
  expect(someNumber).toBe(22);
});

..or at least...

it('should return the appropriate values for the appropriate input ', () => {
  let someNumber;
  someNumber = yourFunction("input A");
  expect(someNumber).toBe(1);

  someNumber = yourFunction("input B");
  expect(someNumber).toBe(-2);

  someNumber = yourFunction("input C");
  expect(someNumber).toBe(22);
});

One of the positive consequences of doing this is that, if your code changes in the future such that, e.g. one (but only one) of the conditions changes (in terms of either input or output), you only need to update one of three simpler tests instead of the single more complicated aggregate test. Additionally, with the tests separated this way, a failing test will more quickly tell you exactly where the problem is, e.g. with "input A", "input B", or "input C".

Alternatively, you may need to actually refactor yourFunction, i.e. the code-under-test itself. Do you really want to have a particular function in your code returning three separate discrete values depending on different input? Perhaps so, but I would examine the code separately to see if it needs to be re-written. It's hard to comment on this further without knowing more details about yourFunction.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...