Skip to content

Commit

Permalink
fix(angular): add jasmine-marbles with a version compatible with inst…
Browse files Browse the repository at this point in the history
…alled rxjs version in migration (#16717)
  • Loading branch information
leosvelperez authored May 3, 2023
1 parent 1e7aa39 commit 9c36989
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import switchToJasmineMarbles from './switch-to-jasmine-marbles';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import {
addProjectConfiguration,
DependencyType,
ProjectGraph,
readJson,
updateJson,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { jasmineMarblesVersion } from '../../utils/versions';
import switchToJasmineMarbles from './switch-to-jasmine-marbles';

let projectGraph: ProjectGraph;
jest.mock('@nx/devkit', () => ({
Expand Down Expand Up @@ -159,4 +160,42 @@ describe('switchToJasmineMarbles', () => {
expect(jasmineMarblesDependency).toBeTruthy();
expect(jasmineMarblesDependency).toBe(jasmineMarblesVersion);
});

it('should add compatible jasmine-marbles version when rxjs version is <7.0.0', async () => {
// ARRANGE
const tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
projectGraph = {
nodes: {},
dependencies: {
test: [
{
type: DependencyType.static,
source: 'test',
target: 'npm:@nrwl/angular',
},
],
},
};
addProjectConfiguration(tree, 'test', {
name: 'test',
root: '',
});
tree.write(
'a/b/mytest.spec.ts',
`import {hot, cold, readFirst} from '@nrwl/angular/testing';`
);
updateJson(tree, 'package.json', (json) => ({
...json,
dependencies: { ...json.dependencies, rxjs: '^6.6.7' },
}));

// ACT
await switchToJasmineMarbles(tree);

// ASSERT

const jasmineMarblesDependency = readJson(tree, 'package.json')
.devDependencies['jasmine-marbles'];
expect(jasmineMarblesDependency).toBe('~0.8.3');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import {
} from '@nx/devkit';
import { extname } from 'path';
import { tsquery } from '@phenomnomnominal/tsquery';
import { jasmineMarblesVersion } from '../../utils/versions';
import {
jasmineMarblesVersion as latestJasmineMarblesVersion,
rxjsVersion as latestRxjsVersion,
} from '../../utils/versions';
import { checkAndCleanWithSemver } from '@nx/devkit/src/utils/semver';
import { gte } from 'semver';

export default async function switchToJasmineMarbles(tree: Tree) {
const usesJasmineMarbles = await replaceJasmineMarbleUsagesInFiles(tree);
Expand Down Expand Up @@ -144,6 +149,8 @@ function addJasmineMarblesDevDependencyIfUsed(
return;
}

const jasmineMarblesVersion = getJasmineMarblesVersion(tree);

addDependenciesToPackageJson(
tree,
{},
Expand All @@ -152,3 +159,18 @@ function addJasmineMarblesDevDependencyIfUsed(
}
);
}

function getJasmineMarblesVersion(tree: Tree): string {
let rxjsVersion: string;
try {
const { dependencies, devDependencies } = readJson(tree, 'package.json');
rxjsVersion = checkAndCleanWithSemver(
'rxjs',
dependencies?.rxjs ?? devDependencies?.rxjs
);
} catch {
rxjsVersion = checkAndCleanWithSemver('rxjs', latestRxjsVersion);
}

return gte(rxjsVersion, '7.0.0') ? latestJasmineMarblesVersion : '~0.8.3';
}

0 comments on commit 9c36989

Please sign in to comment.