question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to mock Windows in `getPlatform` call

See original GitHub issue

Environment

azure-pipelines-task-lib version: 2.9.3

Issue Description

When mocking getPlatform you can’t set a response that indicates Windows.

The mock answers handler returns null if the value of the mock answer isn’t truthy. The Platform enum has Windows as the first entry which means the value of Platform.Windows is zero.

Here’s the rendered JS:

/** Platforms supported by our build agent */
var Platform;
(function (Platform) {
    Platform[Platform["Windows"] = 0] = "Windows";
    Platform[Platform["MacOS"] = 1] = "MacOS";
    Platform[Platform["Linux"] = 2] = "Linux";
})(Platform = exports.Platform || (exports.Platform = {}));

What that means is if you have answers defined:

const answers: ma.TaskLibAnswers = {
  'getPlatform': {
    'getPlatform': tl.Platform.Windows,
  },
};
runner.setAnswers(answers);

Then you’ll see something like this in your debug output:

##vso[task.debug]looking up mock answers for "getPlatform", key '"getPlatform"'
##vso[task.debug]mock response not found

This becomes troublesome for code like this:

export function setConsoleCodePage(): void {
  if (tl.getPlatform() === tl.Platform.Windows) {
    tl.execSync(path.resolve(process.env.windir as string, 'system32', 'chcp.com'), ['65001']);
  }
}

You can work around this by being slightly less explicit:

export function setConsoleCodePage(): void {
  if (!tl.getPlatform()) {
    tl.execSync(path.resolve(process.env.windir as string, 'system32', 'chcp.com'), ['65001']);
  }
}

but it’d be nice if the mocking didn’t drop non-truthy answers.

Hypothetically, this may also drop other non-truthy answers, too but I see things like the checkPath mock, where you’d potentially want to say false to simulate a missing file, handle the falsy values directly.

Expected behaviour

I expect the mock for getPlatform to return 0 (Windows) when configured to mock that value.

Actual behaviour

The mock returns null and indicates no mock is set up for getPlatform.

Steps to reproduce

  1. Create a simple task that just does tl.debug(`Platform: ${tl.getPlatform()}`);
  2. Set up a test that tries to mock the getPlatform call. Set the value to 0 to indicate Windows.
  3. Run the test with the TASK_TEST_TRACE environment variable set.
  4. Inspect the output and notice the value is null rather than 0. Also notice the mock logging indicates there’s no answer rather than an answer indicating 0.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tilligcommented, Aug 20, 2021

Verified, this fixes it. Thanks!

1reaction
kuleshovilyacommented, Aug 20, 2021

The change has been published to npm. Please check when you can. I’ll close ticket for now, please feel free to reopen it if something is wrong or ping me at my email v-ikuleshov@microsoft.com

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I mock the JavaScript 'window' object using Jest?
I would like to mock window's open function, so I can verify the correct URL is passed in to the open function. Using...
Read more >
Invalid hook call after `jest.resetModules` for dynamic `require`s
same issue on my side. Doesn't work for me. The first test is green, others are failing. Asking me to mock all hooks...
Read more >
Mocking window.location in Jest | C.S. Rhymes
I realised it was failing when the window.location was coming back as undefined instead of a string. Somewhere in the code it was...
Read more >
Gracefully handling getters/setters for Jest spies - Joshua Smock
Introduction. Last week at my job we were some writing some new tests for a utility function that depends on calling window.navigator.platform ....
Read more >
Mocking Plugins | Capacitor Documentation
Most mocking libraries create mocks by taking an object and wrapping it in a JavaScript proxy so calls to the methods on that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found