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

Jasmine duplicates test cases to subsequent files #2294

Open
centreboard opened this issue Oct 1, 2020 · 1 comment
Open

Jasmine duplicates test cases to subsequent files #2294

centreboard opened this issue Oct 1, 2020 · 1 comment

Comments

@centreboard
Copy link

Expected Behavior

Test cases are just listed under the file they exist in

Actual Behavior

Test cases are also listed under all subsequent files

I think the issue might be some form of caching in here.

var jasmineInstance = initializeJasmine(Jasmine, projectFolder);
setSpecFilter(jasmineInstance, _ => false);
var testList = [];
testFileList.split(";").forEach((testFile) => {
try {
jasmineInstance.specDir = "";
jasmineInstance.specFiles = [];
jasmineInstance.addSpecFiles([testFile]);
jasmineInstance.loadSpecs();
var topSuite = jasmineInstance.env.topSuite();

If I move the creation of the jasmine instance (Lines 115+116) inside the forEach, i.e. a new one for each file, the test cases only appear for the correct file.

  • NTVS Version: 1.5.20902.1 Commit Hash:b474efcb6f92db52a8f8e2e6a8cb9648476885cc
  • Visual Studio Version: 16.8.0 Preview 3.2
  • Node.js Version: 10.15.0
  • Jasmine: 3.6.1
Steps to Reproduce
  1. Create a .netstandard project following https://docs.microsoft.com/en-us/visualstudio/javascript/unit-testing-javascript-with-visual-studio?view=vs-2019#unit-test-net-core-and-net-standard
  2. Add 2 or more test files
  3. Rebuild project
@colinlobrien
Copy link

The test runner needs to add all the files to jasmine in one call instead of a call for each file.

For those that need a workaround, replace the following two functions in the VS Jasmine test runner file (e.g.: C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\Microsoft\NodeJsTools\TestAdapter\TestFrameworks\Jasmine\jasmine.js).

async function find_tests(testFileList, discoverResultFile, projectFolder) {
    var Jasmine = detectJasmine(projectFolder);
    if (!Jasmine) {
        return;
    }
    
    var jasmineInstance = initializeJasmine(Jasmine, projectFolder);
    setSpecFilter(jasmineInstance, _ => false);

    var testList = [];
	try {
		jasmineInstance.specDir = "";
		jasmineInstance.specFiles = [];

		// In Jasmine 4.0+ addSpecFiles has been deprecated in favor of addMatchingSpecFiles
		(jasmineInstance.addMatchingSpecFiles || jasmineInstance.addSpecFiles).apply(jasmineInstance, [testFileList.split(";")]);
		
		var p = jasmineInstance.loadSpecs();
		if (p instanceof Promise) {
			await p;
		}

		var topSuite = jasmineInstance.env.topSuite();
		// In Jasmine 4.0+ the Suite object is not top level anymore and is instead in the suite_ property
		if (topSuite && topSuite.suite_) {
			topSuite = topSuite.suite_;
		}
		
		enumerateSpecs(topSuite, testList);
	}
	catch (ex) {
		//we would like continue discover other files, so swallow, log and continue;
		console.error("Test discovery error:", ex);
	}

    var fd = fs.openSync(discoverResultFile, 'w');
    fs.writeSync(fd, JSON.stringify(testList));
    fs.closeSync(fd);
}
function enumerateSpecs(suite, testList) {
    suite.children.forEach((child) => {
        if (child instanceof jasmine.Suite) {
            enumerateSpecs(child, testList);
        } else {
            testList.push({
                name: child.description,
                suite: suite.description === "Jasmine__TopLevel__Suite" ? null : suite.getFullName(),
                filepath: child.filename.replace(/^file:\/\/\//i, ''),
                line: 0,
                column: 0
            });
        }
    });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants