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

[WIP] remove lodash #1709

Merged
merged 9 commits into from
Jul 1, 2021
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
4 changes: 4 additions & 0 deletions features/custom_stack_trace.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ Feature: Custom stack trace
"""
When I run cucumber-js
Then it passes
And the error output contains the text:
"""
Warning: unable to get definition line and uri [Custom message]
"""
14 changes: 6 additions & 8 deletions features/step_definitions/message_steps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { find, filter } from 'lodash'
import { Then } from '../../'
import { expect } from 'chai'
import DataTable from '../../src/models/data_table'
Expand All @@ -20,8 +19,7 @@ const ENCODING_MAP: { [key: string]: messages.AttachmentContentEncoding } = {
}

Then('it runs {int} scenarios', function (this: World, expectedCount: number) {
const testCaseStartedEvents = filter(
this.lastRun.envelopes,
const testCaseStartedEvents = this.lastRun.envelopes.filter(
(e) => e.testCaseStarted
)
expect(testCaseStartedEvents).to.have.lengthOf(expectedCount)
Expand Down Expand Up @@ -74,7 +72,7 @@ Then(
this.lastRun.envelopes,
pickleName
)
const testStepResult = find(testStepResults, ['text', stepText])
const testStepResult = testStepResults.find((x) => x.text === stepText)
expect(testStepResult.result.status).to.eql(
status.toUpperCase() as messages.TestStepResultStatus
)
Expand All @@ -95,7 +93,7 @@ Then(
pickleName,
attempt
)
const testStepResult = find(testStepResults, ['text', stepText])
const testStepResult = testStepResults.find((x) => x.text === stepText)
expect(testStepResult.result.status).to.eql(
status.toUpperCase() as messages.TestStepResultStatus
)
Expand All @@ -114,7 +112,7 @@ Then(
this.lastRun.envelopes,
pickleName
)
const testStepResult = find(testStepResults, ['text', hookKeyword])
const testStepResult = testStepResults.find((x) => x.text === hookKeyword)
expect(testStepResult.result.status).to.eql(
status.toUpperCase() as messages.TestStepResultStatus
)
Expand All @@ -133,7 +131,7 @@ Then(
this.lastRun.envelopes,
pickleName
)
const testStepResult = find(testStepResults, ['text', stepText])
const testStepResult = testStepResults.find((x) => x.text === stepText)
if (semver.satisfies(process.version, '>=14.0.0')) {
errorMessage = errorMessage.replace(
'{ member: [Circular] }',
Expand Down Expand Up @@ -161,7 +159,7 @@ Then(
pickleName,
attempt
)
const testStepResult = find(testStepResults, ['text', stepText])
const testStepResult = testStepResults.find((x) => x.text === stepText)
expect(testStepResult.result.status).to.eql(
messages.TestStepResultStatus.FAILED
)
Expand Down
7 changes: 3 additions & 4 deletions features/step_definitions/usage_json_steps.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import _ from 'lodash'
import { DataTable, Then } from '../../'
import { expect } from 'chai'
import path from 'path'
import { World } from '../support/world'
import { IUsage } from '../../src/formatter/helpers/usage_helpers'

Then('it outputs the usage data:', function (this: World, table: DataTable) {
const usageData = JSON.parse(this.lastRun.output)
const usageData: IUsage[] = JSON.parse(this.lastRun.output)
table.hashes().forEach((row: any) => {
const rowUsage = _.find(
usageData,
const rowUsage = usageData.find(
(datum) =>
datum.pattern === row.PATTERN && datum.patternType === row.PATTERN_TYPE
)
Expand Down
9 changes: 4 additions & 5 deletions features/support/formatter_output_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash'
import {
doesHaveValue,
doesNotHaveValue,
Expand Down Expand Up @@ -68,13 +67,13 @@ export function stripMetaMessages(
}

export function normalizeJsonOutput(str: string, cwd: string): IJsonFeature[] {
const json = JSON.parse(valueOrDefault(str, '[]'))
_.each(json, (feature: IJsonFeature) => {
const json: IJsonFeature[] = JSON.parse(valueOrDefault(str, '[]'))
json.forEach((feature: IJsonFeature) => {
if (doesHaveValue(feature.uri)) {
feature.uri = normalizeExceptionAndUri(feature.uri, cwd)
}
_.each(feature.elements, (element: IJsonScenario) => {
_.each(element.steps, (step: IJsonStep) => {
feature.elements.forEach((element: IJsonScenario) => {
element.steps.forEach((step: IJsonStep) => {
if (doesHaveValue(step.match) && doesHaveValue(step.match.location)) {
step.match.location = normalizeExceptionAndUri(
step.match.location,
Expand Down
71 changes: 31 additions & 40 deletions features/support/message_helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _, { Dictionary } from 'lodash'
import { getGherkinStepMap } from '../../src/formatter/helpers/gherkin_document_parser'
import {
getPickleStepMap,
Expand All @@ -18,20 +17,20 @@ export interface IStepTextAndResult {
export function getPickleNamesInOrderOfExecution(
envelopes: messages.Envelope[]
): string[] {
const pickleNameMap: Dictionary<string> = _.chain(envelopes)
.filter((e) => doesHaveValue(e.pickle))
.map((e) => [e.pickle.id, e.pickle.name])
.fromPairs()
.value()
const testCaseToPickleNameMap: Dictionary<string> = _.chain(envelopes)
.filter((e) => doesHaveValue(e.testCase))
.map((e) => [e.testCase.id, pickleNameMap[e.testCase.pickleId]])
.fromPairs()
.value()
return _.chain(envelopes)
.filter((e) => doesHaveValue(e.testCaseStarted))
.map((e) => testCaseToPickleNameMap[e.testCaseStarted.testCaseId])
.value()
const pickleNameMap: Record<string, string> = {}
const testCaseToPickleNameMap: Record<string, string> = {}
const result: string[] = []
envelopes.forEach((e) => {
if (e.pickle != null) {
pickleNameMap[e.pickle.id] = e.pickle.name
} else if (e.testCase != null) {
testCaseToPickleNameMap[e.testCase.id] =
pickleNameMap[e.testCase.pickleId]
} else if (e.testCaseStarted != null) {
result.push(testCaseToPickleNameMap[e.testCaseStarted.testCaseId])
}
})
return result
}

export function getPickleStep(
Expand Down Expand Up @@ -63,18 +62,16 @@ export function getTestStepResults(
const gherkinDocument = getGherkinDocument(envelopes, pickle.uri)
const testCase = getTestCase(envelopes, pickle.id)
const testCaseStarted = getTestCaseStarted(envelopes, testCase.id, attempt)
const testStepIdToResultMap = _.chain(envelopes)
.filter(
(e) =>
doesHaveValue(e.testStepFinished) &&
e.testStepFinished.testCaseStartedId === testCaseStarted.id
)
.map((e) => [
e.testStepFinished.testStepId,
e.testStepFinished.testStepResult,
])
.fromPairs()
.value()
const testStepIdToResultMap: Record<string, messages.TestStepResult> = {}
envelopes.forEach((e) => {
if (
e.testStepFinished != null &&
e.testStepFinished.testCaseStartedId === testCaseStarted.id
) {
testStepIdToResultMap[e.testStepFinished.testStepId] =
e.testStepFinished.testStepResult
}
})
const gherkinStepMap = getGherkinStepMap(gherkinDocument)
const pickleStepMap = getPickleStepMap(pickle)
let isBeforeHook = true
Expand All @@ -101,8 +98,7 @@ export function getTestStepAttachmentsForStep(
const gherkinDocument = getGherkinDocument(envelopes, pickle.uri)
const testCase = getTestCase(envelopes, pickle.id)
const pickleStep = getPickleStepByStepText(pickle, gherkinDocument, stepText)
const testStep = _.find(
testCase.testSteps,
const testStep = testCase.testSteps.find(
(s) => s.pickleStepId === pickleStep.id
)
const testCaseStarted = getTestCaseStarted(envelopes, testCase.id)
Expand All @@ -126,8 +122,7 @@ function getAcceptedPickle(
envelopes: messages.Envelope[],
pickleName: string
): messages.Pickle {
const pickleEnvelope = _.find(
envelopes,
const pickleEnvelope = envelopes.find(
(e) => doesHaveValue(e.pickle) && e.pickle.name === pickleName
)
if (doesNotHaveValue(pickleEnvelope)) {
Expand All @@ -144,8 +139,7 @@ function getGherkinDocument(
envelopes: messages.Envelope[],
uri: string
): messages.GherkinDocument {
const gherkinDocumentEnvelope = _.find(
envelopes,
const gherkinDocumentEnvelope = envelopes.find(
(e) => doesHaveValue(e.gherkinDocument) && e.gherkinDocument.uri === uri
)
if (doesNotHaveValue(gherkinDocumentEnvelope)) {
Expand All @@ -162,8 +156,7 @@ function getTestCase(
envelopes: messages.Envelope[],
pickleId: string
): messages.TestCase {
const testCaseEnvelope = _.find(
envelopes,
const testCaseEnvelope = envelopes.find(
(e) => doesHaveValue(e.testCase) && e.testCase.pickleId === pickleId
)
if (doesNotHaveValue(testCaseEnvelope)) {
Expand All @@ -181,8 +174,7 @@ function getTestCaseStarted(
testCaseId: string,
attempt = 0
): messages.TestCaseStarted {
const testCaseStartedEnvelope = _.find(
envelopes,
const testCaseStartedEnvelope = envelopes.find(
(e) =>
doesHaveValue(e.testCaseStarted) &&
e.testCaseStarted.testCaseId === testCaseId &&
Expand All @@ -204,7 +196,7 @@ function getPickleStepByStepText(
stepText: string
): messages.PickleStep {
const gherkinStepMap = getGherkinStepMap(gherkinDocument)
return _.find(pickle.steps, (s) => {
return pickle.steps.find((s) => {
const keyword = getStepKeyword({ pickleStep: s, gherkinStepMap })
return `${keyword}${s.text}` === stepText
})
Expand All @@ -215,13 +207,12 @@ function getTestStepAttachments(
testCaseStartedId: string,
testStepId: string
): messages.Attachment[] {
return _.chain(envelopes)
return envelopes
.filter(
(e) =>
doesHaveValue(e.attachment) &&
e.attachment.testCaseStartedId === testCaseStartedId &&
e.attachment.testStepId === testStepId
)
.map((e) => e.attachment)
.value()
}
3 changes: 1 addition & 2 deletions features/support/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import colors from 'colors/safe'
import fs from 'fs'
import path from 'path'
import VError from 'verror'
import _ from 'lodash'
import * as messages from '@cucumber/messages'
import * as messageStreams from '@cucumber/message-streams'
import FakeReportServer from '../../test/fake_report_server'
Expand Down Expand Up @@ -63,7 +62,7 @@ export class World {
'--format',
`message:${messageFilename}`,
])
const env = _.merge({}, process.env, this.sharedEnv, envOverride)
const env = { ...process.env, ...this.sharedEnv, ...envOverride }
const cwd = this.tmpDir

let result: IRunResult
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,6 @@
"is-generator": "^1.0.3",
"is-stream": "^2.0.0",
"knuth-shuffle-seeded": "^1.0.6",
"lodash": "^4.17.21",
"mz": "^2.7.0",
"progress": "^2.0.3",
"resolve": "^1.19.0",
Expand All @@ -213,7 +212,6 @@
"@types/express": "4.17.12",
"@types/fs-extra": "9.0.11",
"@types/glob": "7.1.3",
"@types/lodash": "4.14.170",
"@types/mocha": "8.2.2",
"@types/mustache": "4.1.1",
"@types/mz": "2.7.3",
Expand Down
7 changes: 3 additions & 4 deletions src/cli/argv_parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash'
import { Command } from 'commander'
import path from 'path'
import { dialects } from '@cucumber/gherkin'
Expand Down Expand Up @@ -65,10 +64,10 @@ const ArgvParser = {
const e: Error = error
throw new Error(`${option} passed invalid JSON: ${e.message}: ${str}`)
}
if (!_.isPlainObject(val)) {
if (typeof val !== 'object' || Array.isArray(val)) {
throw new Error(`${option} must be passed JSON of an object: ${str}`)
}
return _.merge(memo, val)
return { ...memo, ...val }
}
},

Expand All @@ -85,7 +84,7 @@ const ArgvParser = {
},

validateLanguage(value: string): string {
if (!_.includes(_.keys(dialects), value)) {
if (!Object.keys(dialects).includes(value)) {
throw new Error(`Unsupported ISO 639-1: ${value}`)
}
return value
Expand Down
18 changes: 10 additions & 8 deletions src/cli/configuration_builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash'
import ArgvParser, {
IParsedArgvFormatOptions,
IParsedArgvOptions,
Expand Down Expand Up @@ -133,10 +132,10 @@ export default class ConfigurationBuilder {
}
return [match]
})
return _.flatten(expanded)
return expanded.flat()
}
)
return _.flatten(expandedPaths).map((x) => path.normalize(x))
return expandedPaths.flat().map((x) => path.normalize(x))
}

async expandFeaturePaths(featurePaths: string[]): Promise<string[]> {
Expand All @@ -160,7 +159,7 @@ export default class ConfigurationBuilder {
}
return path.relative(this.cwd, featureDir)
})
return _.uniq(featureDirs)
return [...new Set(featureDirs)]
}

isPublishing(): boolean {
Expand Down Expand Up @@ -192,7 +191,10 @@ export default class ConfigurationBuilder {

mapping[publishUrl] = 'message'
}
return _.map(mapping, (type, outputTo) => ({ outputTo, type }))
return Object.keys(mapping).map((outputTo) => ({
outputTo,
type: mapping[outputTo],
}))
}

isTruthyString(s: string | undefined): boolean {
Expand All @@ -209,13 +211,13 @@ export default class ConfigurationBuilder {
if (filename[0] === '@') {
const filePath = path.join(this.cwd, arg)
const content = await fs.readFile(filePath, 'utf8')
return _.chain(content).split('\n').map(_.trim).value()
return content.split('\n').map((x) => x.trim())
}
return [arg]
})
const featurePaths = _.flatten(nestedFeaturePaths)
const featurePaths = nestedFeaturePaths.flat()
if (featurePaths.length > 0) {
return _.compact(featurePaths)
return featurePaths.filter((x) => x !== '')
}
}
return ['features/**/*.{feature,feature.md}']
Expand Down
3 changes: 1 addition & 2 deletions src/cli/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import _ from 'lodash'
import ArgvParser from './argv_parser'
import ProfileLoader from './profile_loader'
import shuffle from 'knuth-shuffle-seeded'
Expand Down Expand Up @@ -29,7 +28,7 @@ export async function getExpandedArgv({
let fullArgv = argv
const profileArgv = await new ProfileLoader(cwd).getArgv(options.profile)
if (profileArgv.length > 0) {
fullArgv = _.concat(argv.slice(0, 2), profileArgv, argv.slice(2))
fullArgv = argv.slice(0, 2).concat(profileArgv).concat(argv.slice(2))
}
return fullArgv
}
Expand Down
Loading