How to use needsCopyrightHeader method in Jest

Best JavaScript code snippet using jest

Run Jest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

checkCopyrightHeaders.js

Source: checkCopyrightHeaders.js Github

copy
1/**
2 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8'use strict';
9
10const fs = require('fs');
11const {execSync} = require('child_process');
12const {isBinaryFileSync} = require('isbinaryfile');
13
14const getFileContents = path => fs.readFileSync(path, {encoding: 'utf-8'});
15const isDirectory = path => fs.lstatSync(path).isDirectory();
16const createRegExp = pattern => new RegExp(pattern);
17
18// Important: this patterns must be in sync with internal Facebook tools
19
20const GENERIC_IGNORED_EXTENSIONS = [
21  'lock',
22  'patch',
23  'exe',
24  'bin',
25  'cfg',
26  'config',
27  'conf',
28  'html',
29  'md',
30  'markdown',
31  'opam',
32  'osm',
33  'descr',
34  'rst',
35  'json',
36  'key',
37  'ini',
38  'plist',
39  'snap',
40  'svg',
41  'txt',
42  'xcodeproj',
43  'xcscheme',
44  'xml',
45  'yaml',
46  'yml',
47  'textile',
48  'tsv',
49  'csv',
50  'pem',
51  'csr',
52  'der',
53  'crt',
54  'cert',
55  'cer',
56  'p7b',
57  'iml',
58  'org',
59  'podspec',
60  'modulemap',
61  'pch',
62  'lproj',
63  'xcworkspace',
64  'storyboard',
65  'tvml',
66  'xib',
67  'pbxproj',
68  'xcworkspacedata',
69  'xccheckout',
70  'xcsettings',
71  'strings',
72  'ipynb',
73  'htm',
74  'toml',
75].map(extension => createRegExp(`\.${extension}$`));
76
77const GENERIC_IGNORED_PATTERNS = [
78  '(^|/)\\.[^/]+(/|$)',
79
80  'third[_\\-. ]party/',
81  '^node[_\\-. ]modules/',
82  'gradlew\\.bat$',
83  'gradlew$',
84  'gradle/wrapper/',
85  '.idea/',
86  '__init__\\.py$',
87  '^Setup.hs$',
88  '^(readme|README|Readme)\\..*$',
89  'Cargo\\.toml$',
90  '^Cartfile.*$',
91  '^.*\\.xcodeproj/$',
92  '^.*\\.xcworkspace/$',
93  '^.*\\.lproj/$',
94  '^.*\\.bundle/$',
95  '^MANIFEST\\.in$',
96].map(createRegExp);
97
98const CUSTOM_IGNORED_PATTERNS = [
99  '\\.(example|map)$',
100  '^examples/.*',
101  '^flow-typed/.*',
102  '^packages/expect/src/jasmineUtils\\.ts$',
103  '^packages/jest-config/src/vendor/jsonlint\\.js$',
104].map(createRegExp);
105
106const IGNORED_PATTERNS = [
107  ...GENERIC_IGNORED_EXTENSIONS,
108  ...GENERIC_IGNORED_PATTERNS,
109  ...CUSTOM_IGNORED_PATTERNS,
110];
111
112const INCLUDED_PATTERNS = [
113  // Any file with an extension
114  /\.[^/]+$/,
115];
116
117const COPYRIGHT_HEADER =
118  'Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.';
119
120function needsCopyrightHeader(file) {
121  const contents = getFileContents(file);
122  return contents.trim().length > 0 && !contents.includes(COPYRIGHT_HEADER);
123}
124
125function check() {
126  const allFiles = execSync('git ls-files', {encoding: 'utf-8'})
127    .trim()
128    .split('\n');
129
130  const invalidFiles = allFiles.filter(
131    file =>
132      INCLUDED_PATTERNS.some(pattern => pattern.test(file)) &&
133      !IGNORED_PATTERNS.some(pattern => pattern.test(file)) &&
134      !isDirectory(file) &&
135      !isBinaryFileSync(file) &&
136      needsCopyrightHeader(file)
137  );
138
139  if (invalidFiles.length > 0) {
140    console.log(`Facebook copyright header check failed for the following files:
141
142  ${invalidFiles.join('\n  ')}
143
144Please include the header or blacklist the files in \`scripts/checkCopyrightHeaders.js\``);
145    process.exit(1);
146  }
147}
148
149check();
150
Full Screen

checkLicenseHeaders.js

Source: checkLicenseHeaders.js Github

copy
1/*
2 * Copyright (c) 2018, salesforce.com, inc.
3 * All rights reserved.
4 * SPDX-License-Identifier: MIT
5 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
6 */
7
8// Borrowed from https://github.com/facebook/jest
9
10/**
11 * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
12 *
13 * This source code is licensed under the MIT license found in the
14 * LICENSE file in the root directory of this source tree.
15 */
16
17'use strict';
18
19const fs = require('fs');
20const {execSync} = require('child_process');
21const {isBinaryFileSync} = require('isbinaryfile');
22
23const getFileContents = path => fs.readFileSync(path, {encoding: 'utf-8'});
24const isDirectory = path => fs.lstatSync(path).isDirectory();
25const createRegExp = pattern => new RegExp(pattern);
26
27const IGNORED_EXTENSIONS = [
28    'lock',
29    'patch',
30    'exe',
31    'bin',
32    'cfg',
33    'config',
34    'conf',
35    // 'html', // might have LWC components we want the header on
36    'md',
37    'markdown',
38    'opam',
39    'osm',
40    'descr',
41    'rst',
42    'json',
43    'key',
44    'ini',
45    'plist',
46    'snap',
47    'svg',
48    'txt',
49    'xcodeproj',
50    'xcscheme',
51    'xml',
52    'yaml',
53    'yml',
54    'textile',
55    'tsv',
56    'csv',
57    'pem',
58    'csr',
59    'der',
60    'crt',
61    'cert',
62    'cer',
63    'p7b',
64    'iml',
65    'org',
66    'podspec',
67    'modulemap',
68    'pch',
69    'lproj',
70    'xcworkspace',
71    'storyboard',
72    'tvml',
73    'xib',
74    'pbxproj',
75    'xcworkspacedata',
76    'xccheckout',
77    'xcsettings',
78    'strings',
79    'ipynb',
80    'htm',
81    'toml',
82].map(extension => createRegExp(`\.${extension}$`));
83
84const GENERIC_IGNORED_PATTERNS = [
85    '(^|/)\\.[^/]+(/|$)',
86
87    //'third[_\\-. ]party/',  // to be on the safe side
88    '^node[_\\-. ]modules/',
89    'gradlew\\.bat$',
90    'gradlew$',
91    'gradle/wrapper/',
92    '.idea/',
93    '__init__\\.py$',
94    '^Setup.hs$',
95    '^(readme|README|Readme)\\..*$',
96    'Cargo\\.toml$',
97    '^Cartfile.*$',
98    '^.*\\.xcodeproj/$',
99    '^.*\\.xcworkspace/$',
100    '^.*\\.lproj/$',
101    '^.*\\.bundle/$',
102    '^MANIFEST\\.in$',
103].map(createRegExp);
104
105const CUSTOM_IGNORED_PATTERNS = [
106    // add anything repo specific here
107].map(createRegExp);
108
109const IGNORED_PATTERNS = [
110  ...IGNORED_EXTENSIONS,
111  ...GENERIC_IGNORED_PATTERNS,
112  ...CUSTOM_IGNORED_PATTERNS,
113];
114
115const INCLUDED_PATTERNS = [
116  // Any file with an extension
117  /\.[^/]+$/,
118];
119
120const COPYRIGHT_HEADER_RE = /Copyright (\(c\))? [0-9]{4}, (s|S)alesforce.com, inc./;
121
122function needsCopyrightHeader(file) {
123  const contents = getFileContents(file);
124  return contents.trim().length > 0 && !COPYRIGHT_HEADER_RE.test(contents);
125}
126
127function check() {
128  const allFiles = execSync('git ls-files', {encoding: 'utf-8'})
129    .trim()
130    .split('\n');
131
132  const invalidFiles = allFiles.filter(
133    file =>
134      INCLUDED_PATTERNS.some(pattern => pattern.test(file)) &&
135      !IGNORED_PATTERNS.some(pattern => pattern.test(file)) &&
136      !isDirectory(file) &&
137      !isBinaryFileSync(file) &&
138      needsCopyrightHeader(file)
139  );
140
141  if (invalidFiles.length > 0) {
142    console.log(`Salesforce copyright header check failed for the following files:
143  ${invalidFiles.join('\n  ')}
144Please include the header or blacklist the files in \`scripts/checkCopyrightHeaders.js\``);
145    process.exit(1);
146  }
147}
148
149check();
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Jest on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)