UNPKG

@sentry/wizard

Version:

Sentry wizard helping you to configure your project

187 lines 10.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); var ast_utils_1 = require("../../src/utils/ast-utils"); var recast = __importStar(require("recast")); var b = recast.types.builders; describe('hasSentryContent', function () { it.each([ "\n const { sentryVitePlugin } = require(\"@sentry/vite-plugin\");\n const somethingelse = require('gs');\n ", "\n import { sentryVitePlugin } from \"@sentry/vite-plugin\";\n import * as somethingelse from 'gs';\n\n export default {\n plugins: [sentryVitePlugin()]\n }\n ", ])("returns true if a require('@sentry/') call was found in the parsed module", function (code) { // recast.parse returns a Program node (or fails) but it's badly typed as any // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access var program = recast.parse(code) .program; expect((0, ast_utils_1.hasSentryContent)(program)).toBe(true); }); it.each([ "const whatever = require('something')", "// const {sentryWebpackPlugin} = require('@sentry/webpack-plugin')", "const {sAntryWebpackPlugin} = require('webpack-plugin-@sentry')", "\n import * as somethingelse from 'gs';\n export default {\n plugins: []\n }\n ", "import * as somethingelse from 'gs';\n // import { sentryVitePlugin } from \"@sentry/vite-plugin\"\n export default {\n plugins: []\n }\n ", "import * as thirdPartyVitePlugin from \"vite-plugin-@sentry\"\n export default {\n plugins: [thirdPartyVitePlugin()]\n }\n ", ])("returns false if the file doesn't contain any require('@sentry/') calls", function (code) { // recast.parse returns a Program node (or fails) but it's badly typed as any // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access var program = recast.parse(code) .program; expect((0, ast_utils_1.hasSentryContent)(program)).toBe(false); }); }); describe('getObjectProperty', function () { it.each([ [ 'literal', b.objectExpression([ b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')), b.objectProperty(b.stringLiteral('needle'), b.stringLiteral('haystack')), ]), ], [ 'stringLiteral', b.objectExpression([ b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')), b.objectProperty(b.literal('needle'), b.stringLiteral('haystack')), ]), ], [ 'identifier', b.objectExpression([ b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')), b.objectProperty(b.identifier('needle'), b.stringLiteral('haystack')), ]), ], ])('returns the poperty (%s) if it exists', function (_, object) { var property = (0, ast_utils_1.getObjectProperty)(object, 'needle'); expect(property).toBeDefined(); // @ts-expect-error we know it's defined due to the expect above expect(recast.print(property).code).toEqual(expect.stringContaining('needle')); }); it('returns undefined if the property does not exist', function () { var object = b.objectExpression([ b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')), ]); var property = (0, ast_utils_1.getObjectProperty)(object, 'needle'); expect(property).toBeUndefined(); }); it('handles objects without simple properties', function () { var object = b.objectExpression([b.spreadElement(b.identifier('foo'))]); var property = (0, ast_utils_1.getObjectProperty)(object, 'needle'); expect(property).toBeUndefined(); }); }); describe('getOrSetObjectProperty', function () { it('returns the property if it exists', function () { var object = b.objectExpression([ b.objectProperty(b.identifier('needle'), b.stringLiteral('haystack')), ]); var property = (0, ast_utils_1.getOrSetObjectProperty)(object, 'needle', b.stringLiteral('nope')); expect(property).toBeDefined(); expect(property.type).toBe('ObjectProperty'); // @ts-expect-error we know its type expect(property.key.name).toBe('needle'); // @ts-expect-error we know its type expect(property.value.value).toBe('haystack'); }); it('adds the property if it does not exist', function () { var object = b.objectExpression([ b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')), ]); var property = (0, ast_utils_1.getOrSetObjectProperty)(object, 'needle', b.stringLiteral('haystack')); expect(property).toBeDefined(); expect(property.type).toBe('Property'); // @ts-expect-error we know its type expect(property.key.value).toBe('needle'); // @ts-expect-error we know its type expect(property.value.value).toBe('haystack'); }); }); describe('setOrUpdateObjectProperty', function () { it('sets a new property if it does not exist yet', function () { var object = b.objectExpression([ b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')), ]); (0, ast_utils_1.setOrUpdateObjectProperty)(object, 'needle', b.stringLiteral('haystack')); expect((0, ast_utils_1.getObjectProperty)(object, 'needle')).toBeDefined(); }); it('updates an existing property if it exists', function () { var object = b.objectExpression([ b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')), b.objectProperty(b.identifier('needle'), b.stringLiteral('haystack')), ]); (0, ast_utils_1.setOrUpdateObjectProperty)(object, 'needle', b.stringLiteral('haystack2')); var property = (0, ast_utils_1.getObjectProperty)(object, 'needle'); // @ts-expect-error it must be defiend, otherwise we fail anyway expect(property === null || property === void 0 ? void 0 : property.value.value).toBe('haystack2'); }); it('adds a comment to the existing property if provided', function () { var object = b.objectExpression([ b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')), ]); (0, ast_utils_1.setOrUpdateObjectProperty)(object, 'needle', b.stringLiteral('haystack'), 'This is a comment'); var property = (0, ast_utils_1.getObjectProperty)(object, 'needle'); expect(property === null || property === void 0 ? void 0 : property.comments).toHaveLength(1); // @ts-expect-error it must be defiend, otherwise we fail anyway expect(property === null || property === void 0 ? void 0 : property.comments[0].value).toBe(' This is a comment'); }); it('adds a comment to the new property if provided', function () { var object = b.objectExpression([ b.objectProperty(b.identifier('foo'), b.stringLiteral('bar')), ]); (0, ast_utils_1.setOrUpdateObjectProperty)(object, 'needle', b.stringLiteral('haystack'), 'This is a comment'); var property = (0, ast_utils_1.getObjectProperty)(object, 'needle'); expect(property === null || property === void 0 ? void 0 : property.comments).toHaveLength(1); // @ts-expect-error it must be defiend, otherwise we fail anyway expect(property === null || property === void 0 ? void 0 : property.comments[0].value).toBe(' This is a comment'); }); }); describe('parse and print JSON-C', function () { it.each([ ['simple JSON', "{'foo': 'bar'}"], [ 'JSON-C with inline comment', "\n {\n \"foo\": \"bar\" // with an inline comment\n }\n ", ], [ 'JSON-C with multiple comments', "\n /*\n * let's throw in a block comment for good measure\n */ \n {\n // one line comment\n \"foo\": \"bar\", // another inline comment\n /* one more here */\n \"dogs\": /* and here */ \"awesome\",\n }\n /* and here */\n ", ], ])("parses and prints JSON-C (%s)", function (_, json) { var _a = (0, ast_utils_1.parseJsonC)(json), ast = _a.ast, jsonObject = _a.jsonObject; expect(ast === null || ast === void 0 ? void 0 : ast.type).toBe('Program'); expect(jsonObject).toBeDefined(); expect(jsonObject === null || jsonObject === void 0 ? void 0 : jsonObject.type).toBe('ObjectExpression'); // @ts-expect-error we know it's defined due to the expect above expect((0, ast_utils_1.printJsonC)(ast)).toEqual(json); }); it('returns undefined if the input is not valid JSON-C', function () { var _a = (0, ast_utils_1.parseJsonC)("{\n \"invalid\": // \"json\"\n }"), ast = _a.ast, jsonObject = _a.jsonObject; expect(ast).toBeUndefined(); expect(jsonObject).toBeUndefined(); }); }); //# sourceMappingURL=ast-utils.test.js.map