Best JavaScript code snippet using best
it.spec.js
Source:it.spec.js
1/**2 * The MIT License (MIT)3 *4 * Copyright (c) 2016-2020 Mickael Jeanroy5 *6 * Permission is hereby granted, free of charge, to any person obtaining a copy7 * of this software and associated documentation files (the "Software"), to deal8 * in the Software without restriction, including without limitation the rights9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell10 * copies of the Software, and to permit persons to whom the Software is11 * furnished to do so, subject to the following conditions:12 *13 * The above copyright notice and this permission notice shall be included in all14 * copies or substantial portions of the Software.15 *16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE22 * SOFTWARE.23 */24import path from 'path';25import tmp from 'tmp';26import fs from 'fs';27import _ from 'lodash';28import * as rollup from 'rollup';29import nodeResolve from '@rollup/plugin-node-resolve';30import commonjs from '@rollup/plugin-commonjs';31import licensePlugin from '../../src/index.js';32import {join} from '../utils/join.js';33describe('rollup-plugin-license', () => {34 let warn;35 let tmpDir;36 beforeEach(() => {37 warn = spyOn(console, 'warn');38 });39 beforeEach(() => {40 tmpDir = tmp.dirSync({41 unsafeCleanup: true,42 });43 });44 afterEach(() => {45 tmpDir.removeCallback();46 });47 it('should generate bundle with dependency output', (done) => {48 const thirdPartyOutput = path.join(tmpDir.name, 'dependencies.txt');49 const rollupConfig = createRollupConfig({50 thirdParty: {51 output: thirdPartyOutput,52 },53 });54 writeBundle(rollupConfig).then(() => {55 verifyFile(thirdPartyOutput, done, (data) => {56 expect(data.toString()).toContain('lodash');57 });58 });59 });60 it('should generate bundle with dependency output as an object', (done) => {61 const thirdPartyOutput = path.join(tmpDir.name, 'dependencies.txt');62 const rollupConfig = createRollupConfig({63 thirdParty: {64 output: {65 file: thirdPartyOutput,66 },67 },68 });69 writeBundle(rollupConfig).then(() => {70 verifyFile(thirdPartyOutput, done, (data) => {71 expect(data.toString()).toContain('lodash');72 });73 });74 });75 it('should generate bundle with dependency output as a JSON file', (done) => {76 const thirdPartyOutput = path.join(tmpDir.name, 'dependencies.json');77 const rollupConfig = createRollupConfig({78 thirdParty: {79 output: {80 file: thirdPartyOutput,81 template(dependencies) {82 return JSON.stringify(dependencies);83 },84 },85 },86 });87 writeBundle(rollupConfig).then(() => {88 verifyFile(thirdPartyOutput, done, (data) => {89 const content = data.toString();90 const json = JSON.parse(content);91 expect(json).toBeDefined();92 expect(json.length).toBe(1);93 expect(json[0].name).toBe('lodash');94 });95 });96 });97 it('should generate bundle with dependency output as a JSON & a text file', (done) => {98 const jsonOutput = path.join(tmpDir.name, 'dependencies.json');99 const txtOutput = path.join(tmpDir.name, 'dependencies.json');100 const rollupConfig = createRollupConfig({101 thirdParty: {102 output: [103 {104 file: txtOutput,105 },106 {107 file: jsonOutput,108 template(dependencies) {109 return JSON.stringify(dependencies);110 },111 },112 ],113 },114 });115 writeBundle(rollupConfig).then(() => {116 const onDone = _.after(2, () => done());117 onDone.fail = (err) => done.fail(err);118 verifyFile(jsonOutput, onDone, (data) => {119 const content = data.toString();120 const json = JSON.parse(content);121 expect(json).toBeDefined();122 expect(json.length).toBe(1);123 expect(json[0].name).toBe('lodash');124 });125 verifyFile(txtOutput, onDone, (data) => {126 const content = data.toString();127 expect(content).toBeDefined();128 expect(content).toContain('lodash');129 });130 });131 });132 it('should generate bundle and export dependencies to given function', (done) => {133 const thirdPartyOutput = jasmine.createSpy('thirdPartyOutput');134 const rollupConfig = createRollupConfig({135 thirdParty: {136 output: thirdPartyOutput,137 },138 });139 writeBundle(rollupConfig).then(() => {140 expect(thirdPartyOutput).toHaveBeenCalledWith([141 jasmine.objectContaining({142 name: 'lodash',143 }),144 ]);145 done();146 });147 });148 it('should generate bundle with license header', (done) => {149 const banner = 'test banner';150 const rollupConfig = createRollupConfig({151 banner,152 });153 writeBundle(rollupConfig).then(() => {154 verifyFile(rollupConfig.output.file, done, (data) => {155 expect(data.toString()).toContain(join([156 `/**`,157 ` * ${banner}`,158 ` */`,159 ]));160 });161 });162 });163 it('should generate bundle with license header from content as a raw string', (done) => {164 const content = 'Banner from inline content';165 const banner = {content};166 const rollupConfig = createRollupConfig({167 banner,168 });169 writeBundle(rollupConfig).then(() => {170 verifyFile(rollupConfig.output.file, done, (data) => {171 expect(warn).not.toHaveBeenCalled();172 expect(data.toString()).toContain(join([173 `/**`,174 ` * ${content}`,175 ` */`,176 ]));177 });178 });179 });180 it('should generate bundle with license header from content as a string returned from function', (done) => {181 const content = 'Banner from inline content';182 const banner = {183 content() {184 return content;185 },186 };187 const rollupConfig = createRollupConfig({188 banner,189 });190 writeBundle(rollupConfig).then(() => {191 verifyFile(rollupConfig.output.file, done, (data) => {192 expect(warn).not.toHaveBeenCalled();193 expect(data.toString()).toContain(join([194 `/**`,195 ` * ${content}`,196 ` */`,197 ]));198 });199 });200 });201 it('should generate bundle with license header from given content file', (done) => {202 const banner = {203 content: {204 file: path.join(__dirname, '..', 'fixtures', 'banner.txt'),205 encoding: 'utf-8',206 },207 };208 const rollupConfig = createRollupConfig({209 banner,210 });211 writeBundle(rollupConfig).then(() => {212 verifyFile(rollupConfig.output.file, done, (data) => {213 expect(warn).not.toHaveBeenCalled();214 expect(data.toString()).toContain(join([215 '/**',216 ' * Test banner.',217 ' *',218 ' * With a second line.',219 ' */',220 ]));221 });222 });223 });224 function createRollupConfig(licensePluginOptions) {225 return {226 input: path.join(__dirname, 'bundle.js'),227 output: {228 file: path.join(tmpDir.name, 'bundle.js'),229 format: 'es',230 },231 plugins: [232 nodeResolve(),233 commonjs(),234 licensePlugin(235 licensePluginOptions236 ),237 ],238 };239 }240 function writeBundle(rollupConfig) {241 return rollup.rollup(rollupConfig).then((bundle) => bundle.write(rollupConfig.output));242 }243 function verifyFile(file, done, test) {244 fs.readFile(file, 'utf8', (err, data) => {245 if (err) {246 done.fail(err);247 }248 test(data);249 done();250 });251 }...
setupTypeScript.js
Source:setupTypeScript.js
1// @ts-check2/** This script modifies the project to support TS code in .svelte files like:3 <script lang="ts">4 export let name: string;5 </script>6 7 As well as validating the code for CI.8 */9/** To work on this script:10 rm -rf test-template template && git clone sveltejs/template test-template && node scripts/setupTypeScript.js test-template11*/12const fs = require("fs")13const path = require("path")14const { argv } = require("process")15const projectRoot = argv[2] || path.join(__dirname, "..")16// Add deps to pkg.json17const packageJSON = JSON.parse(fs.readFileSync(path.join(projectRoot, "package.json"), "utf8"))18packageJSON.devDependencies = Object.assign(packageJSON.devDependencies, {19 "svelte-check": "^1.0.0",20 "svelte-preprocess": "^4.0.0",21 "@rollup/plugin-typescript": "^6.0.0",22 "typescript": "^3.9.3",23 "tslib": "^2.0.0",24 "@tsconfig/svelte": "^1.0.0"25})26// Add script for checking27packageJSON.scripts = Object.assign(packageJSON.scripts, {28 "validate": "svelte-check"29})30// Write the package JSON31fs.writeFileSync(path.join(projectRoot, "package.json"), JSON.stringify(packageJSON, null, " "))32// mv src/main.js to main.ts - note, we need to edit rollup.config.js for this too33const beforeMainJSPath = path.join(projectRoot, "src", "main.js")34const afterMainTSPath = path.join(projectRoot, "src", "main.ts")35fs.renameSync(beforeMainJSPath, afterMainTSPath)36// Switch the app.svelte file to use TS37const appSveltePath = path.join(projectRoot, "src", "App.svelte")38let appFile = fs.readFileSync(appSveltePath, "utf8")39appFile = appFile.replace("<script>", '<script lang="ts">')40appFile = appFile.replace("export let name;", 'export let name: string;')41fs.writeFileSync(appSveltePath, appFile)42// Edit rollup config43const rollupConfigPath = path.join(projectRoot, "rollup.config.js")44let rollupConfig = fs.readFileSync(rollupConfigPath, "utf8")45// Edit imports46rollupConfig = rollupConfig.replace(`'rollup-plugin-terser';`, `'rollup-plugin-terser';47import sveltePreprocess from 'svelte-preprocess';48import typescript from '@rollup/plugin-typescript';`)49// Replace name of entry point50rollupConfig = rollupConfig.replace(`'src/main.js'`, `'src/main.ts'`)51// Add preprocess to the svelte config, this is tricky because there's no easy signifier.52// Instead we look for `css:` then the next `}` and add the preprocessor to that53let foundCSS = false54let match55// https://regex101.com/r/OtNjwo/156const configEditor = new RegExp(/css:.|\n*}/gmi)57while (( match = configEditor.exec(rollupConfig)) != null) {58 if (foundCSS) {59 const endOfCSSIndex = match.index + 160 rollupConfig = rollupConfig.slice(0, endOfCSSIndex) + ",\n preprocess: sveltePreprocess()," + rollupConfig.slice(endOfCSSIndex);61 break62 }63 if (match[0].includes("css:")) foundCSS = true64}65// Add TypeScript66rollupConfig = rollupConfig.replace(67 'commonjs(),',68 'commonjs(),\n\t\ttypescript({\n\t\t\tsourceMap: !production,\n\t\t\tinlineSources: !production\n\t\t}),'69);70fs.writeFileSync(rollupConfigPath, rollupConfig)71// Add TSConfig72const tsconfig = `{73 "extends": "@tsconfig/svelte/tsconfig.json",74 "include": ["src/**/*"],75 "exclude": ["node_modules/*", "__sapper__/*", "public/*"]76}`77const tsconfigPath = path.join(projectRoot, "tsconfig.json")78fs.writeFileSync(tsconfigPath, tsconfig)79// Delete this script, but not during testing80if (!argv[2]) {81 // Remove the script82 fs.unlinkSync(path.join(__filename))83 // Check for Mac's DS_store file, and if it's the only one left remove it84 const remainingFiles = fs.readdirSync(path.join(__dirname))85 if (remainingFiles.length === 1 && remainingFiles[0] === '.DS_store') {86 fs.unlinkSync(path.join(__dirname, '.DS_store'))87 }88 // Check if the scripts folder is empty89 if (fs.readdirSync(path.join(__dirname)).length === 0) {90 // Remove the scripts folder91 fs.rmdirSync(path.join(__dirname))92 }93}94// Adds the extension recommendation95fs.mkdirSync(path.join(projectRoot, ".vscode"))96fs.writeFileSync(path.join(projectRoot, ".vscode", "extensions.json"), `{97 "recommendations": ["svelte.svelte-vscode"]98}99`)100console.log("Converted to TypeScript.")101if (fs.existsSync(path.join(projectRoot, "node_modules"))) {102 console.log("\nYou will need to re-run your dependency manager to get started.")...
js.js
Source:js.js
1'use strict';2if (!config.tasks.js) {3 return false;4}5const gulpRollup = require('gulp-rollup');6const nodeResolve = require('rollup-plugin-node-resolve');7const includePaths = require('rollup-plugin-includepaths');8const rollupSourcemaps = require('rollup-plugin-sourcemaps');9const commonjs = require('rollup-plugin-commonjs');10const buble = require('rollup-plugin-buble');11const globals = require('rollup-plugin-node-globals');12const babel = require('rollup-plugin-babel');13const { uglify } = require('rollup-plugin-uglify');14const { minify } = require('uglify-es');15let rollupConfig = config.tasks.js.rollup;16let paths = {17 src: path.join(config.root.base, config.root.src, config.tasks.js.src, getExtensions(config.tasks.js.extensions)),18 dest: path.join(config.root.base, config.root.dest, config.tasks.js.dest)19};20if (rollupConfig) {21 if (rollupConfig.plugins.includePaths.paths.length && rollupConfig.plugins.includePaths.paths[0] == '') {22 rollupConfig.plugins.includePaths.paths[0] = path.join(config.root.base, config.root.src);23 }24}25let rollupPlugins = [26 includePaths(rollupConfig.plugins.includePaths),27 nodeResolve(rollupConfig.plugins.nodeResolve)28];29if (rollupConfig.plugins.alias) {30 const alias = require('rollup-plugin-alias');31 for (let key of Object.keys(rollupConfig.plugins.alias)) {32 const value = rollupConfig.plugins.alias[key];33 rollupConfig.plugins.alias[key] = path.resolve(...value);34 }35 rollupPlugins.push(alias(rollupConfig.plugins.alias));36}37if (rollupConfig.plugins.commonjs) {38 if (typeof rollupConfig.plugins.commonjs == 'boolean') {39 rollupPlugins.push(commonjs());40 } else {41 rollupPlugins.push(commonjs(rollupConfig.plugins.commonjs));42 }43}44if (rollupConfig.plugins.amd) {45 const amd = require('rollup-plugin-amd');46 if (typeof rollupConfig.plugins.amd == 'boolean') {47 rollupPlugins.push(amd());48 } else {49 rollupPlugins.push(amd(rollupConfig.plugins.amd));50 }51}52rollupPlugins.push(rollupSourcemaps());53rollupPlugins.push(globals());54if (rollupConfig.buble) { rollupPlugins.push(buble()); }55if (!rollupConfig.buble) { rollupPlugins.push(babel({56 exclude: 'node_modules/**'57})); }58console.log(uglify);59if (mode.minimize) { rollupPlugins.push(uglify({mangle : true}, minify)); }60function js() {61 return gulp.src(paths.src, {since: cache.lastMtime('js')})62 .pipe(plumber(handleErrors))63 .pipe(mode.maps ? sourcemaps.init({loadMaps: true}) : util.noop())64 .pipe(gulpRollup({65 rollup: require('rollup'),66 input: new Promise((resolve, reject) => {67 glob(paths.src, (error, files) => {68 resolve(files);69 });70 }),71 output: {72 format: rollupConfig.format73 },74 allowRealFiles: true,75 plugins: rollupPlugins,76 }))77 .pipe(config.root.inlineAssets ? gulp.dest(path.join(config.root.base, config.root.inlineAssets)) : util.noop())78 .pipe(config.banner ? header(config.banner, {79 info: config.info,80 timestamp: getTimestamp()81 }) : util.noop())82 .pipe(mode.maps ? sourcemaps.write() : util.noop())83 .pipe(gulp.dest(paths.dest))84 .pipe(browserSync ? browserSync.stream() : util.noop())85 .pipe(size({86 title: 'JS:',87 showFiles: true88 }));89}...
Using AI Code Generation
1const BestRollup = require('bestrollup');2const bestRollup = new BestRollup();3bestRollup.rollupConfig({4 output: {5 }6});7const BestRollup = require('bestrollup');8const bestRollup = new BestRollup();9bestRollup.rollupConfig({10 output: {11 }12});13const BestRollup = require('bestrollup');14const bestRollup = new BestRollup();15bestRollup.rollupConfig({16 output: {17 }18});19const BestRollup = require('bestrollup');20const bestRollup = new BestRollup();21bestRollup.rollupConfig({22 output: {23 }24});25const BestRollup = require('bestrollup');26const bestRollup = new BestRollup();27bestRollup.rollupConfig({28 output: {29 }30});31const BestRollup = require('bestrollup');32const bestRollup = new BestRollup();33bestRollup.rollupConfig({34 output: {35 }36});
Using AI Code Generation
1const BestPractice = require('./BestPractice');2const config = BestPractice.rollupConfig({3 output: {4 }5});6module.exports = config;7const BestPractice = require('./BestPractice');8const config = BestPractice.rollupConfig({9 output: {10 }11});12module.exports = config;13const BestPractice = require('./BestPractice');14const config = BestPractice.rollupConfig({15 output: {16 }17});18module.exports = config;19const BestPractice = require('./BestPractice');20const config = BestPractice.rollupConfig({21 output: {22 }23});24module.exports = config;25const BestPractice = require('./BestPractice');26const config = BestPractice.rollupConfig({27 output: {28 }29});30module.exports = config;31const BestPractice = require('./BestPractice');32const config = BestPractice.rollupConfig({33 output: {34 }35});36module.exports = config;37const BestPractice = require('./BestPractice');38const config = BestPractice.rollupConfig({39 output: {40 }41});
Using AI Code Generation
1const rollupConfig = require('rollup-config');2module.exports = rollupConfig({3});4const rollupConfig = require('rollup-config');5module.exports = rollupConfig({6});7const rollupConfig = require('rollup-config');8module.exports = rollupConfig({9});10const rollupConfig = require('rollup-config');11module.exports = rollupConfig({12});13const rollupConfig = require('rollup-config');14module.exports = rollupConfig({15});16const rollupConfig = require('rollup-config');17module.exports = rollupConfig({18});19const rollupConfig = require('rollup-config');20module.exports = rollupConfig({21});22const rollupConfig = require('rollup-config');23module.exports = rollupConfig({24});25const rollupConfig = require('rollup-config');26module.exports = rollupConfig({
Using AI Code Generation
1import rollupConfig from 'rollup-config-best-practice';2const config = rollupConfig({3 output: {4 }5});6export default config;7import rollupConfig from 'rollup-config-best-practice';8const config = rollupConfig({9 output: {10 }11});12export default config;13import rollupConfig from 'rollup-config-best-practice';14const config = rollupConfig({15 output: {16 }17});18export default config;19import rollupConfig from 'rollup-config-best-practice';20const config = rollupConfig({21 output: {22 }23});24export default config;25import rollupConfig from 'rollup-config-best-practice';26const config = rollupConfig({27 output: {28 }29});30export default config;31import rollupConfig from 'rollup-config-best-practice';32const config = rollupConfig({33 output: {
Using AI Code Generation
1const {BestPractice} = require('best-practice');2const bestPractice = new BestPractice();3bestPractice.rollupConfig('test4.js');4const {BestPractice} = require('best-practice');5const bestPractice = new BestPractice();6bestPractice.rollupConfig('test4.js');7const {BestPractice} = require('best-practice');8const bestPractice = new BestPractice();9bestPractice.rollupConfig('test4.js');10const {BestPractice} = require('best-practice');11const bestPractice = new BestPractice();12bestPractice.rollupConfig('test4.js');13const {BestPractice} = require('best-practice');14const bestPractice = new BestPractice();15bestPractice.rollupConfig('test4.js');16const {BestPractice} = require('best-practice');17const bestPractice = new BestPractice();18bestPractice.rollupConfig('test4.js');19const {BestPractice} = require('best-practice');20const bestPractice = new BestPractice();21bestPractice.rollupConfig('test4.js');22const {BestPractice} = require('best-practice');23const bestPractice = new BestPractice();24bestPractice.rollupConfig('test4.js');25const {BestPractice} = require('best-practice');
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!