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

[identity] Use esm4mocha, re-enable IBC test, remove unnecessary stub #28580

Merged
merged 8 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions common/tools/dev-tool/src/commands/run/testNodeJSInput.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license

import concurrently from "concurrently";
import { leafCommand, makeCommandInfo } from "../../framework/command";

import concurrently from "concurrently";
import { createPrinter } from "../../util/printer";
import { isModuleProject } from "../../util/resolveProject";
import { runTestsWithProxyTool } from "../../util/testUtils";
Expand All @@ -17,17 +18,31 @@ export const commandInfo = makeCommandInfo(
default: false,
description: "whether to run with test-proxy",
},
"use-esm-workaround": {
shortName: "esm",
kind: "boolean",
maorleger marked this conversation as resolved.
Show resolved Hide resolved
default: true,
description:
"when true, uses the `esm` npm package for tests. Otherwise uses esm4mocha if needed",
},
},
);

export default leafCommand(commandInfo, async (options) => {
const isModule = await isModuleProject();
let esmLoaderArgs = "";

if (isModule === false) {
if (options["use-esm-workaround"] === false) {
esmLoaderArgs = "--loader=../../../common/tools/esm4mocha.mjs";
} else {
esmLoaderArgs = "-r ../../../common/tools/esm-workaround -r esm";
}
}

const reporterArgs =
"--reporter ../../../common/tools/mocha-multi-reporter.js --reporter-option output=test-results.xml";
const defaultMochaArgs = `${
(await isModuleProject())
? "-r source-map-support/register.js"
: "-r ../../../common/tools/esm-workaround -r esm -r source-map-support/register"
} ${reporterArgs} --full-trace`;
const defaultMochaArgs = `${esmLoaderArgs} -r source-map-support/register.js ${reporterArgs} --full-trace`;
const updatedArgs = options["--"]?.map((opt) =>
opt.includes("**") && !opt.startsWith("'") && !opt.startsWith('"') ? `"${opt}"` : opt,
);
Expand Down
8 changes: 5 additions & 3 deletions common/tools/esm4mocha.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

import { readFile, stat, constants } from "node:fs/promises";
import { constants, readFile, stat } from "node:fs/promises";
import { dirname, join } from "node:path";
import { fileURLToPath } from "url";

import { Project } from "./dev-tool/node_modules/ts-morph/dist/ts-morph.js";
import { fileURLToPath } from "url";

// if modules are loaded from dist-esm/ treat them as ESM
export async function resolve(specifier, context, defaultResolve) {
Expand Down Expand Up @@ -32,7 +33,8 @@ export async function load(url, context, defaultLoad) {
};
}
const { source } = await defaultLoad(url, context, defaultLoad);
return { format: context.format, source, shortCircuit: true };
const format = context.format ?? "builtin";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

node:fs/promises wasn't importing correctly for me. I was thinking it's fine to default to builtin? Alternatively I can also do something like:

const format = context.format
if (format === undefined && url.startsWith("node:")) format = "builtin"

Or if anyone has a better suggestion, I am not too familiar with loader logic

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

interesting! My changes are barely minimum to get many packages test running. I am sure there are cases that aren't handled properly. If buildin works we will use it!

return { format, source, shortCircuit: true };
}

async function updateSpecifierValueIfRelative(declaration, base) {
Expand Down
5 changes: 2 additions & 3 deletions sdk/identity/identity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"format": "dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
"check-format": "dev-tool run vendored prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
"integration-test:browser": "echo skipped",
"integration-test:node": "dev-tool run test:node-js-input -- --timeout 180000 'dist-esm/test/public/node/*.spec.js' 'dist-esm/test/internal/node/*.spec.js'",
"integration-test:node": "dev-tool run test:node-js-input --use-esm-workaround=false -- --timeout 180000 'dist-esm/test/public/node/*.spec.js' 'dist-esm/test/internal/node/*.spec.js'",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"lint:fix": "eslint package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]",
"lint": "eslint package.json api-extractor.json src test --ext .ts",
Expand Down Expand Up @@ -159,7 +159,6 @@
"sinon": "^17.0.0",
"ts-node": "^10.0.0",
"typescript": "~5.3.3",
"util": "^0.12.1",
"esm": "^3.2.18"
"util": "^0.12.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ describe("ClientAssertionCredential (internal)", function () {
await credential.getToken("https://vault.azure.net/.default");
} catch (e: any) {
// We're ignoring errors since our main goal here is to ensure that we send the correct parameters to MSAL.
console.log("error", e);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds noise, if we're ignoring it let's ignore it

}

assert.equal(getTokenSilentSpy.callCount, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe("ClientCertificateCredential (internal)", function () {
});

it("throws when given a file that doesn't contain a PEM-formatted certificate", async function (this: Context) {
const fullPath = path.resolve(__dirname, "../src/index.ts");
const fullPath = path.resolve("./clientCertificateCredential.spec.ts");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__dirname is not supported, but it doesn't actually matter what file we send as long as it's not a proper cert

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah __dirname is blocking some packages' test too. There's recommendation of const __dirname=path.dirname(url.fileURLToPath(import.meta.url) but import.meta.url requires "module" of "ES2020", which is at odds with our module of commonjs for test:node-ts-input :(

const credential = new ClientCertificateCredential("tenant", "client", {
certificatePath: fullPath,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain */
/* eslint-disable @typescript-eslint/no-namespace */

import { MsalTestCleanup, msalNodeTestSetup } from "../../node/msalNodeTestSetup";
import { Recorder, env } from "@azure-tools/test-recorder";
import { Context } from "mocha";
import {
InteractiveBrowserCredential,
InteractiveBrowserCredentialNodeOptions,
} from "../../../src";
import { MsalTestCleanup, msalNodeTestSetup } from "../../node/msalNodeTestSetup";
import { Recorder, env } from "@azure-tools/test-recorder";

import { Context } from "mocha";
import Sinon from "sinon";
import { assert } from "chai";
import http from "http";
Expand Down Expand Up @@ -46,9 +46,7 @@ describe("InteractiveBrowserCredential (internal)", function () {

const scope = "https://vault.azure.net/.default";

// TODO: re-enable this when we resolve the esm incompatibility issues
// https://github.com/Azure/azure-sdk-for-js/issues/28373
it.skip("Throws an expected error if no browser is available", async function (this: Context) {
it("Throws an expected error if no browser is available", async function (this: Context) {
// The SinonStub type does not include this second parameter to throws().
const testErrorMessage = "No browsers available on this test.";
(sandbox.stub(interactiveBrowserMockable, "open") as any).throws(
Expand Down
10 changes: 5 additions & 5 deletions sdk/identity/identity/test/node/msalNodeTestSetup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import * as util from "../../src/msal/utils";

import {
AuthenticationResult,
ConfidentialClientApplication,
Expand Down Expand Up @@ -46,9 +44,6 @@ export async function msalNodeTestSetup(

const sandbox = createSandbox();

const stub = sandbox.stub(util, "randomUUID");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a better world to live in, no weird wrapping and no concerns with immutable modules

stub.returns(playbackValues.correlationId);

if (testContextOrStubbedToken instanceof Test || testContextOrStubbedToken === undefined) {
const testContext = testContextOrStubbedToken;

Expand Down Expand Up @@ -132,6 +127,11 @@ export async function msalNodeTestSetup(
target: `x-client-VER=[a-zA-Z0-9.-]+`,
value: `x-client-VER=identity-client-version`,
},
{
regex: true,
target: `client-request-id=[a-zA-Z0-9-]+`,
value: `client-request-id=${playbackValues.correlationId}`,
},
],
bodyKeySanitizers: [
{
Expand Down
3 changes: 2 additions & 1 deletion sdk/identity/identity/test/public/node/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Licensed under the MIT license.

import * as fs from "fs";
import * as jwt from "jsonwebtoken";
import * as net from "net";
import * as tls from "tls";

import jwt from "jsonwebtoken";
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be the default export to work

import ms from "ms";
import { randomUUID } from "@azure/core-util";

Expand Down