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.

How to set variable in unit test of task

See original GitHub issue

I try to set variables before run a task in a unit test

Environment

azure-pipelines-task-lib version: 2.9.3

Issue Description

I tried to set variables in the unit test with

import * as fs from 'fs';
import * as path from 'path';
import { TaskMockRunner } from 'azure-pipelines-task-lib/mock-run';
import { TaskLibAnswers } from 'azure-pipelines-task-lib/mock-answer';

const task: TaskMockRunner = new TaskMockRunner(taskPath);
task.setInput('excelFilePath', excelFilePath);
task.setVariableName('Build.BinariesDirectory', __dirname);
task.setVariableName('Product.Version', '0.1.0');

// Mock answer for excel file path
let answers: TaskLibAnswers = <TaskLibAnswers>{
	"checkPath": {
		[excelFilePath]: fs.existsSync(excelFilePath)
	}
};
task.setAnswers(answers);

task.run();

And in the task i get variables in run function with :

import { TaskResult, VariableInfo, getPathInput, setResult, getVariables } from 'azure-pipelines-task-lib/task';
...
let variables: VariableInfo[] = getVariables();

but the array is always empty

Expected behaviour

The variables array should contains ‘Build.BinariesDirectory’ and ‘Product.Version’

Actual behaviour

The array is always empty I also tried with:

task.setVariableName('BUILD_BINARIESDIRECTORY', __dirname);
task.setVariableName('PRODUCT_VERSION', '0.1.0');

or

process.env['Build.BinariesDirectory'] = __dirname;
process.env['Product.Version'] = '0.1.0';

or

process.env['BUILD_BINARIESDIRECTORY'] = __dirname;
process.env['PRODUCT_VERSION'] = '0.1.0';

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
jtpettycommented, Jan 9, 2020

ok, I think I understand now. I have a work around for you. The issue appears that when you use a method from task-lib in your unit test, it does some initialization and then the mocking for task.setInput no longer works.

Here is a work around to set the variables and have getVariables() return the right thing:

process.env['PRODUCT_VERSION'] = '0.1.0';
process.env['BUILD_BINARIESDIRECTORY'] = __dirname;
process.env['VSTS_PUBLIC_VARIABLES'] = '["Product.Version","Build.BinariesDirectory"]';
0reactions
CodeTrooperscommented, Jan 14, 2020

Yes, thanks for your help

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set variable name after method execution in Unit test
First you want to assign a value in the callback. == is not assigning a value it is an equality comparison. Second no...
Read more >
How to write unitTest for checking variables value?
In userTask I used form and in unitTest I wrote complete(task) and I checked variable for that form I created before. something like...
Read more >
Unit testing fundamentals - Visual Studio (Windows)
From the code editor window, right-click and choose Create Unit Tests from the right-click menu.
Read more >
Best Practices - Apache Airflow
You can write unit tests for both your tasks and your DAG. ... Make sure your DAG is parameterized to change the variables,...
Read more >
Using xUnit to Test your C# Code - Auth0
How to create unit tests and integration tests with xUnit for your C# ... [Fact] public async Task GetGlossaryList() { // Act var...
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