Best K6 code snippet using tc39.compile
tc39_test.go
Source:tc39_test.go
...16 "time"17 "github.com/dop251/goja"18 "github.com/dop251/goja/parser"19 "github.com/stretchr/testify/assert"20 "go.k6.io/k6/js/compiler"21 "go.k6.io/k6/lib"22 "go.k6.io/k6/lib/testutils"23 "gopkg.in/yaml.v3"24)25const (26 tc39BASE = "TestTC39/test262"27)28//nolint:gochecknoglobals29var (30 errInvalidFormat = errors.New("invalid file format")31 // ignorableTestError = newSymbol(stringEmpty)32 sabStub = goja.MustCompile("sabStub.js", `33 Object.defineProperty(this, "SharedArrayBuffer", {34 get: function() {35 throw IgnorableTestError;36 }37 });`,38 false)39 featuresBlockList = []string{40 "BigInt", // not supported at all41 "IsHTMLDDA", // not supported at all42 "generators", // not supported in a meaningful way IMO43 "Array.prototype.item", // not even standard yet44 "async-iteration", // not supported at all45 "TypedArray.prototype.item", // not even standard yet46 "String.prototype.replaceAll", // not supported at all, Stage 4 since 202047 // from goja48 "Symbol.asyncIterator",49 "async-functions",50 "class-static-block",51 "class-fields-private",52 "class-fields-private-in",53 "regexp-named-groups",54 "regexp-dotall",55 "regexp-unicode-property-escapes",56 "regexp-unicode-property-escapes",57 "regexp-match-indices",58 "legacy-regexp",59 "tail-call-optimization",60 "Temporal",61 "import-assertions",62 "dynamic-import",63 "logical-assignment-operators",64 "coalesce-expression",65 "import.meta",66 "Atomics",67 "Atomics.waitAsync",68 "FinalizationRegistry",69 "WeakRef",70 "numeric-separator-literal",71 "Object.fromEntries",72 "Object.hasOwn",73 "__getter__",74 "__setter__",75 "ShadowRealm",76 "SharedArrayBuffer",77 "error-cause",78 "resizable-arraybuffer", // stage 3 as of 2021 https://github.com/tc39/proposal-resizablearraybuffer79 "array-find-from-last", // stage 3 as of 2021 https://github.com/tc39/proposal-array-find-from-last80 "Array.prototype.at", // stage 3 as of 2021 https://github.com/tc39/proposal-relative-indexing-method81 "String.prototype.at", // stage 3 as of 2021 https://github.com/tc39/proposal-relative-indexing-method82 "TypedArray.prototype.at", // stage 3 as of 2021 https://github.com/tc39/proposal-relative-indexing-method83 }84 skipWords = []string{"yield", "generator", "Generator"}85 skipList = map[string]bool{86 "test/built-ins/Function/prototype/toString/AsyncFunction.js": true,87 "test/built-ins/Object/seal/seal-generatorfunction.js": true,88 "test/built-ins/Date/parse/without-utc-offset.js": true, // some other reason ?!? depending on local time89 "test/built-ins/Array/prototype/concat/arg-length-exceeding-integer-limit.js": true, // takes forever and is broken90 "test/built-ins/Array/prototype/splice/throws-if-integer-limit-exceeded.js": true, // takes forever and is broken91 "test/built-ins/Array/prototype/unshift/clamps-to-integer-limit.js": true, // takes forever and is broken92 "test/built-ins/Array/prototype/unshift/throws-if-integer-limit-exceeded.js": true, // takes forever and is broken93 }94 pathBasedBlock = []string{ // This completely skips any path matching it without any kind of message95 "test/annexB/built-ins/Date",96 "test/annexB/built-ins/RegExp/prototype/Symbol.split",97 "test/annexB/built-ins/String/prototype/anchor",98 "test/annexB/built-ins/String/prototype/big",99 "test/annexB/built-ins/String/prototype/blink",100 "test/annexB/built-ins/String/prototype/bold",101 "test/annexB/built-ins/String/prototype/fixed",102 "test/annexB/built-ins/String/prototype/fontcolor",103 "test/annexB/built-ins/String/prototype/fontsize",104 "test/annexB/built-ins/String/prototype/italics",105 "test/annexB/built-ins/String/prototype/link",106 "test/annexB/built-ins/String/prototype/small",107 "test/annexB/built-ins/String/prototype/strike",108 "test/annexB/built-ins/String/prototype/sub",109 "test/annexB/built-ins/String/prototype/sup",110 "test/annexB/built-ins/RegExp/legacy-accessors/",111 // Async/Promise and other totally unsupported functionality112 "test/built-ins/AsyncArrowFunction",113 "test/built-ins/AsyncFromSyncIteratorPrototype",114 "test/built-ins/AsyncFunction",115 "test/built-ins/AsyncGeneratorFunction",116 "test/built-ins/AsyncGeneratorPrototype",117 "test/built-ins/AsyncIteratorPrototype",118 "test/built-ins/Atomics",119 "test/built-ins/BigInt",120 "test/built-ins/SharedArrayBuffer",121 "test/language/eval-code/direct/async",122 "test/language/expressions/await",123 "test/language/expressions/async",124 "test/language/expressions/dynamic-import",125 "test/language/expressions/object/dstr/async",126 "test/language/module-code/top-level-await",127 "test/language/statements/async-function",128 "test/built-ins/Function/prototype/toString/async",129 "test/built-ins/Function/prototype/toString/async",130 "test/built-ins/Function/prototype/toString/generator",131 "test/built-ins/Function/prototype/toString/proxy-async",132 "test/built-ins/FinalizationRegistry", // still in proposal133 "test/built-ins/RegExp/property-escapes", // none of those work134 "test/language/identifiers/start-unicode", // tests whether some unicode can be used in identifiers, half don't work, take forever135 "test/built-ins/Object/prototype/__lookup", // AnnexB lookupGetter lookupSetter136 "test/built-ins/Object/prototype/__define", // AnnexB defineGetter defineSetter137 }138)139//nolint:unused,structcheck140type tc39Test struct {141 name string142 f func(t *testing.T)143}144type tc39BenchmarkItem struct {145 name string146 duration time.Duration147}148type tc39BenchmarkData []tc39BenchmarkItem149type tc39TestCtx struct {150 compilerPool *compiler.Pool151 base string152 t *testing.T153 prgCache map[string]*goja.Program154 prgCacheLock sync.Mutex155 enableBench bool156 benchmark tc39BenchmarkData157 benchLock sync.Mutex158 testQueue []tc39Test //nolint:unused,structcheck159 expectedErrors map[string]string160 errorsLock sync.Mutex161 errors map[string]string162}163type TC39MetaNegative struct {164 Phase, Type string165}166type tc39Meta struct {167 Negative TC39MetaNegative168 Includes []string169 Flags []string170 Features []string171 Es5id string172 Es6id string173 Esid string174}175func (m *tc39Meta) hasFlag(flag string) bool {176 for _, f := range m.Flags {177 if f == flag {178 return true179 }180 }181 return false182}183func parseTC39File(name string) (*tc39Meta, string, error) {184 f, err := os.Open(name) //nolint:gosec185 if err != nil {186 return nil, "", err187 }188 defer f.Close() //nolint:errcheck,gosec189 b, err := ioutil.ReadAll(f)190 if err != nil {191 return nil, "", err192 }193 metaStart := bytes.Index(b, []byte("/*---"))194 if metaStart == -1 {195 return nil, "", errInvalidFormat196 }197 metaStart += 5198 metaEnd := bytes.Index(b, []byte("---*/"))199 if metaEnd == -1 || metaEnd <= metaStart {200 return nil, "", errInvalidFormat201 }202 var meta tc39Meta203 err = yaml.Unmarshal(b[metaStart:metaEnd], &meta)204 if err != nil {205 return nil, "", err206 }207 if meta.Negative.Type != "" && meta.Negative.Phase == "" {208 return nil, "", errors.New("negative type is set, but phase isn't")209 }210 return &meta, string(b), nil211}212func (*tc39TestCtx) detachArrayBuffer(call goja.FunctionCall) goja.Value {213 if obj, ok := call.Argument(0).(*goja.Object); ok {214 var buf goja.ArrayBuffer215 if goja.New().ExportTo(obj, &buf) == nil {216 // if buf, ok := obj.Export().(goja.ArrayBuffer); ok {217 buf.Detach()218 return goja.Undefined()219 }220 }221 panic(goja.New().NewTypeError("detachArrayBuffer() is called with incompatible argument"))222}223func (ctx *tc39TestCtx) fail(t testing.TB, name string, strict bool, errStr string) {224 nameKey := fmt.Sprintf("%s-strict:%v", name, strict)225 expected, ok := ctx.expectedErrors[nameKey]226 if index := strings.LastIndex(errStr, " at "); index != -1 {227 errStr = errStr[:index] + " <at omitted>"228 }229 if ok {230 if !assert.Equal(t, expected, errStr) {231 ctx.errorsLock.Lock()232 ctx.errors[nameKey] = errStr233 ctx.errorsLock.Unlock()234 }235 } else {236 assert.Empty(t, errStr)237 ctx.errorsLock.Lock()238 ctx.errors[nameKey] = errStr239 ctx.errorsLock.Unlock()240 }241}242func (ctx *tc39TestCtx) runTC39Test(t testing.TB, name, src string, meta *tc39Meta, strict bool) {243 if skipList[name] {244 t.Skip("Excluded")245 }246 failf := func(str string, args ...interface{}) {247 str = fmt.Sprintf(str, args...)248 ctx.fail(t, name, strict, str)249 }250 defer func() {251 if x := recover(); x != nil {252 failf("panic while running %s: %v", name, x)253 }254 }()255 vm := goja.New()256 _262 := vm.NewObject()257 ignorableTestError := vm.NewGoError(fmt.Errorf(""))258 vm.Set("IgnorableTestError", ignorableTestError)259 _ = _262.Set("detachArrayBuffer", ctx.detachArrayBuffer)260 _ = _262.Set("createRealm", func(goja.FunctionCall) goja.Value {261 panic(ignorableTestError)262 })263 vm.Set("$262", _262)264 vm.Set("print", t.Log)265 _, err := vm.RunProgram(sabStub)266 if err != nil {267 panic(err)268 }269 if strict {270 src = "'use strict';\n" + src271 }272 var out []string273 async := meta.hasFlag("async") //nolint:ifshort // false positive274 if async {275 err = ctx.runFile(ctx.base, path.Join("harness", "doneprintHandle.js"), vm)276 if err != nil {277 t.Fatal(err)278 }279 _ = vm.Set("print", func(msg string) {280 out = append(out, msg)281 })282 } else {283 _ = vm.Set("print", t.Log)284 }285 early, origErr, err := ctx.runTC39Script(name, src, meta.Includes, vm)286 if err == nil {287 if meta.Negative.Type != "" {288 // vm.vm.prg.dumpCode(t.Logf)289 failf("%s: Expected error: %v", name, err)290 return291 }292 nameKey := fmt.Sprintf("%s-strict:%v", name, strict)293 expected, ok := ctx.expectedErrors[nameKey]294 assert.False(t, ok, "%s passes but and error %q was expected", nameKey, expected)295 return296 }297 if meta.Negative.Type == "" {298 if err, ok := err.(*goja.Exception); ok {299 if err.Value() == ignorableTestError {300 t.Skip("Test threw IgnorableTestError")301 }302 }303 failf("%s: %v", name, err)304 return305 }306 if meta.Negative.Phase == "early" && !early || meta.Negative.Phase == "runtime" && early {307 failf("%s: error %v happened at the wrong phase (expected %s)", name, err, meta.Negative.Phase)308 return309 }310 errType := getErrType(name, err, failf)311 if errType != "" && errType != meta.Negative.Type {312 if meta.Negative.Type == "SyntaxError" && origErr != nil && getErrType(name, origErr, failf) == meta.Negative.Type {313 return314 }315 // vm.vm.prg.dumpCode(t.Logf)316 failf("%s: unexpected error type (%s), expected (%s)", name, errType, meta.Negative.Type)317 return318 }319 /*320 if vm.vm.sp != 0 {321 t.Fatalf("sp: %d", vm.vm.sp)322 }323 if l := len(vm.vm.iterStack); l > 0 {324 t.Fatalf("iter stack is not empty: %d", l)325 }326 */327 if async {328 complete := false329 for _, line := range out {330 if strings.HasPrefix(line, "Test262:AsyncTestFailure:") {331 t.Fatal(line)332 } else if line == "Test262:AsyncTestComplete" {333 complete = true334 }335 }336 if !complete {337 for _, line := range out {338 t.Log(line)339 }340 t.Fatal("Test262:AsyncTestComplete was not printed")341 }342 }343}344func getErrType(name string, err error, failf func(str string, args ...interface{})) string {345 switch err := err.(type) {346 case *goja.Exception:347 if o, ok := err.Value().(*goja.Object); ok { //nolint:nestif348 if c := o.Get("constructor"); c != nil {349 if c, ok := c.(*goja.Object); ok {350 return c.Get("name").String()351 } else {352 failf("%s: error constructor is not an object (%v)", name, o)353 return ""354 }355 } else {356 failf("%s: error does not have a constructor (%v)", name, o)357 return ""358 }359 } else {360 failf("%s: error is not an object (%v)", name, err.Value())361 return ""362 }363 case *goja.CompilerSyntaxError, *parser.Error, parser.ErrorList:364 return "SyntaxError"365 case *goja.CompilerReferenceError:366 return "ReferenceError"367 default:368 failf("%s: error is not a JS error: %v", name, err)369 return ""370 }371}372func shouldBeSkipped(t testing.TB, meta *tc39Meta) {373 for _, feature := range meta.Features {374 for _, bl := range featuresBlockList {375 if feature == bl {376 t.Skipf("Blocklisted feature %s", feature)377 }378 }379 }380}381func (ctx *tc39TestCtx) runTC39File(name string, t testing.TB) {382 p := path.Join(ctx.base, name)383 meta, src, err := parseTC39File(p)384 if err != nil {385 // t.Fatalf("Could not parse %s: %v", name, err)386 t.Errorf("Could not parse %s: %v", name, err)387 return388 }389 shouldBeSkipped(t, meta)390 var startTime time.Time391 if ctx.enableBench {392 startTime = time.Now()393 }394 hasRaw := meta.hasFlag("raw")395 /*396 if hasRaw || !meta.hasFlag("onlyStrict") {397 // log.Printf("Running normal test: %s", name)398 // t.Logf("Running normal test: %s", name)399 ctx.runTC39Test(t, name, src, meta, false)400 }401 */402 if !hasRaw && !meta.hasFlag("noStrict") {403 // log.Printf("Running strict test: %s", name)404 // t.Logf("Running strict test: %s", name)405 ctx.runTC39Test(t, name, src, meta, true)406 } else { // Run test in non strict mode only if we won't run them in strict407 // TODO uncomment the if above and delete this else so we run both parts when the tests408 // don't take forever409 ctx.runTC39Test(t, name, src, meta, false)410 }411 if ctx.enableBench {412 ctx.benchLock.Lock()413 ctx.benchmark = append(ctx.benchmark, tc39BenchmarkItem{414 name: name,415 duration: time.Since(startTime),416 })417 ctx.benchLock.Unlock()418 }419}420func (ctx *tc39TestCtx) init() {421 ctx.prgCache = make(map[string]*goja.Program)422 ctx.errors = make(map[string]string)423 b, err := ioutil.ReadFile("./breaking_test_errors.json")424 if err != nil {425 panic(err)426 }427 b = bytes.TrimSpace(b)428 if len(b) > 0 {429 ctx.expectedErrors = make(map[string]string, 1000)430 err = json.Unmarshal(b, &ctx.expectedErrors)431 if err != nil {432 panic(err)433 }434 }435}436func (ctx *tc39TestCtx) compile(base, name string) (*goja.Program, error) {437 ctx.prgCacheLock.Lock()438 defer ctx.prgCacheLock.Unlock()439 prg := ctx.prgCache[name]440 if prg == nil {441 fname := path.Join(base, name)442 f, err := os.Open(fname) //nolint:gosec443 if err != nil {444 return nil, err445 }446 defer f.Close() //nolint:gosec,errcheck447 b, err := ioutil.ReadAll(f)448 if err != nil {449 return nil, err450 }451 str := string(b)452 comp := ctx.compilerPool.Get()453 defer ctx.compilerPool.Put(comp)454 comp.Options = compiler.Options{Strict: false, CompatibilityMode: lib.CompatibilityModeExtended}455 prg, _, err = comp.Compile(str, name, true)456 if err != nil {457 return nil, err458 }459 ctx.prgCache[name] = prg460 }461 return prg, nil462}463func (ctx *tc39TestCtx) runFile(base, name string, vm *goja.Runtime) error {464 prg, err := ctx.compile(base, name)465 if err != nil {466 return err467 }468 _, err = vm.RunProgram(prg)469 return err470}471func (ctx *tc39TestCtx) runTC39Script(name, src string, includes []string, vm *goja.Runtime) (early bool, origErr, err error) {472 early = true473 err = ctx.runFile(ctx.base, path.Join("harness", "assert.js"), vm)474 if err != nil {475 return476 }477 err = ctx.runFile(ctx.base, path.Join("harness", "sta.js"), vm)478 if err != nil {479 return480 }481 for _, include := range includes {482 err = ctx.runFile(ctx.base, path.Join("harness", include), vm)483 if err != nil {484 return485 }486 }487 var p *goja.Program488 comp := ctx.compilerPool.Get()489 defer ctx.compilerPool.Put(comp)490 comp.Options = compiler.Options{Strict: false, CompatibilityMode: lib.CompatibilityModeBase}491 p, _, origErr = comp.Compile(src, name, true)492 if origErr != nil {493 src, _, err = comp.Transform(src, name, nil)494 if err == nil {495 p, _, err = comp.Compile(src, name, true)496 }497 } else {498 err = origErr499 }500 if err != nil {501 return502 }503 early = false504 _, err = vm.RunProgram(p)505 return506}507func (ctx *tc39TestCtx) runTC39Tests(name string) {508 files, err := ioutil.ReadDir(path.Join(ctx.base, name))509 if err != nil {510 ctx.t.Fatal(err)511 }512outer:513 for _, file := range files {514 if file.Name()[0] == '.' {515 continue516 }517 newName := path.Join(name, file.Name())518 for _, skipWord := range skipWords {519 if strings.Contains(newName, skipWord) {520 ctx.t.Run(newName, func(t *testing.T) {521 t.Skipf("Skip %s because %s is not supported", newName, skipWord)522 })523 continue outer524 }525 }526 for _, path := range pathBasedBlock { // TODO: use trie / binary search?527 if strings.HasPrefix(newName, path) {528 ctx.t.Run(newName, func(t *testing.T) {529 t.Skipf("Skip %s because of path based block", newName)530 })531 continue outer532 }533 }534 if file.IsDir() {535 ctx.runTC39Tests(newName)536 } else if strings.HasSuffix(file.Name(), ".js") && !strings.HasSuffix(file.Name(), "_FIXTURE.js") {537 ctx.runTest(newName, func(t *testing.T) {538 ctx.runTC39File(newName, t)539 })540 }541 }542}543func TestTC39(t *testing.T) {544 if testing.Short() {545 t.Skip()546 }547 if _, err := os.Stat(tc39BASE); err != nil {548 t.Skipf("If you want to run tc39 tests, you need to run the 'checkout.sh` script in the directory to get https://github.com/tc39/test262 at the correct last tested commit (%v)", err)549 }550 ctx := &tc39TestCtx{551 base: tc39BASE,552 compilerPool: compiler.NewPool(testutils.NewLogger(t), runtime.GOMAXPROCS(0)),553 }554 ctx.init()555 // ctx.enableBench = true556 t.Run("test262", func(t *testing.T) {557 ctx.t = t558 ctx.runTC39Tests("test/language")559 ctx.runTC39Tests("test/built-ins")560 ctx.runTC39Tests("test/harness")561 ctx.runTC39Tests("test/annexB/built-ins")562 ctx.flush()563 })564 if ctx.enableBench {565 sort.Slice(ctx.benchmark, func(i, j int) bool {566 return ctx.benchmark[i].duration > ctx.benchmark[j].duration...
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!!